从 API 的 json 响应中检索数据

我试图通过将数据存储在某些结构(机场+坐标)中来从 json 响应中获取数据,但我不知道如何处理它,因为我对地图和界面不够好。代码没有显示任何错误,但 MapofAirports 完全是空的,代码如下:


package main


import (

    //"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) }


回首忆惘然
浏览 76回答 1
1回答

慕沐林林

这是我的最后一次更新,在运行程序时,它在解组步骤中出现恐慌package mainimport (    "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)}这有什么问题吗?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go