亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 API 的 json 響應中檢索數據

從 API 的 json 響應中檢索數據

Go
回首憶惘然 2023-08-07 10:52:39
我試圖通過將數據存儲在某些結構(機場+坐標)中來從 json 響應中獲取數據,但我不知道如何處理它,因為我對地圖和界面不夠好。代碼沒有顯示任何錯誤,但 MapofAirports 完全是空的,代碼如下:package mainimport (? ? //"api/client"? ? //"api/client/clienterrors"? ? //"api/client/openstreetmap"? ? "encoding/json"? ? "fmt"? ? "io/ioutil"? ? "log"? ? "math"? ? "net/http"? ? "os"? ? "strconv"? ? "strings")type Coordinates struct {? ? Longitude string `json:"lon"`? ? Latitude? string `json:"lat"`}type Airport struct {? ? Co? ? ? ?Coordinates `json:"location"`? ? IATACode string? ? ? `json:"id"`? ? Distance float64? ? ?`json:"distance"` // distance to coordinates in kilometer}func GetCoordinatesFromURL(url string) (float64, float64) {? ? parts := strings.Split(url, "=")? ? lat0 := strings.Split(parts[2], "&")? ? lon0 := strings.Split(parts[3], "&")? ? lat1, _ := strconv.ParseFloat(lat0[0], 64)? ? lon1, _ := strconv.ParseFloat(lon0[0], 64)? ? return lat1, lon1}func CalcDistance(lat1 float64, long1 float64, lat2 float64, long2 float64) float64 {? ? var latitude1 = lat1 * math.Pi / 180? ? var latitude2 = lat2 * math.Pi / 180? ? var longitude1 = long1 * math.Pi / 180? ? var longitude2 = long2 * math.Pi / 180? ? var R = 6371.0? ? var d = R * math.Acos(math.Cos(latitude1)*math.Cos(latitude2)*math.Cos(longitude2-longitude1)+math.Sin(latitude1)*math.Sin(latitude2))? ? return d}func main() {? ? var Locations []Airport? ? Locations = make([]Airport, 0)? ? var url = fmt.Sprintf("https://api.skypicker.com/locations?type=radius&lat=40.730610&lon=-73.935242&radius=250&location_types=airport&limit=3&sort=id&active_only=true")? ? UrlLat, UrlLon := GetCoordinatesFromURL(url)? ? resp, err := http.Get(url)? ? if err != nil {? ? ? ? panic(err.Error())? ? }我正在這樣處理:{ locations[],meta,last_refresh,results_retrieved } ==> location : { id , location + distance(calculated with a function) }
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

這是我的最后一次更新,在運行程序時,它在解組步驟中出現恐慌


package main


import (

    "encoding/json"

    "fmt"

    "io/ioutil"

    "math"

    "net/http"

    "strconv"

    "strings"

)


type Coordinates struct {

    Longitude string `json:"lon"`

    Latitude  string `json:"lat"`

}


type Airport struct {

    Co       Coordinates `json:"location"`

    IATACode string      `json:"id"`

    Distance float64     `json:"distance"` // distance to coordinates in kilometer

}


type Response struct {

    Locations []Airport `json:"locations"`

    // add all the other fields you care about

}


func GetCoordinatesFromURL(url string) (float64, float64) {


    parts := strings.Split(url, "=")


    lat0 := strings.Split(parts[2], "&")

    lon0 := strings.Split(parts[3], "&")


    lat1, _ := strconv.ParseFloat(lat0[0], 64)

    lon1, _ := strconv.ParseFloat(lon0[0], 64)


    return lat1, lon1

}


func CalcDistance(lat1 float64, long1 float64, lat2 float64, long2 float64) float64 {


    var latitude1 = lat1 * math.Pi / 180

    var latitude2 = lat2 * math.Pi / 180

    var longitude1 = long1 * math.Pi / 180

    var longitude2 = long2 * math.Pi / 180


    var R = 6371.0

    var d = R * math.Acos(math.Cos(latitude1)*math.Cos(latitude2)*math.Cos(longitude2-longitude1)+math.Sin(latitude1)*math.Sin(latitude2))


    return d

}


func main() {



    var url = fmt.Sprintf("https://api.skypicker.com/locations?type=radius&lat=40.730610&lon=-73.935242&radius=250&location_types=airport&limit=3&sort=id&active_only=true")


    UrlLat, UrlLon := GetCoordinatesFromURL(url)


    resp, err := http.Get(url)

    if err != nil {

        panic(err.Error())

    }

    defer resp.Body.Close()


     data, err := ioutil.ReadAll(resp.Body)

    res := &Response{}

    if err := json.Unmarshal(data, res); err != nil {

        panic(err)

    }

    fmt.Println(res.Locations)


    for i, item := range res.Locations {

        latt,_ := strconv.ParseFloat(item.Co.Latitude, 64)

        lonn,_ :=strconv.ParseFloat(item.Co.Longitude, 64)

        res.Locations[i].Distance = CalcDistance(latt,lonn , UrlLat, UrlLon)

    }

    fmt.Println("after calculate distance")

    fmt.Println(res.Locations)

}

這有什么問題嗎?


查看完整回答
反對 回復 2023-08-07
  • 1 回答
  • 0 關注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號