为了账号安全,请及时绑定邮箱和手机立即绑定

从 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 关注
  • 75 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信