json - 使用 App Engine 在 golang 中将 json 解析为来自 google api 请求的结构

标签 json google-app-engine go urlfetch

我实际上是在尝试在 golang 中使用 google map api(在将 urlfetch 与应用引擎一起使用时,当我执行查询时,我无法在结构中获得结果。

我的代码

import (

   "google.golang.org/appengine"
   "google.golang.org/appengine/log"
   "google.golang.org/appengine/urlfetch"
   "net/http"
   "strings"
   "encoding/json"
    "encoding/gob"
    "bytes"
)

func GetCoordinatesByAddress(request *http.Request) bool {

    var results Results

    ctx := appengine.NewContext(request)
    client := urlfetch.Client(ctx)

    resp, err := client.Get("https://maps.googleapis.com/maps/api/geocode/json?address=Suresnes+France"&key=" + ApiKey)
    if err != nil {
      return false
    }

    decoder := json.NewDecoder(resp.Body)
    decoder.Decode(&results)
    log.Debugf(ctx, "", results)
}

type Results struct {
   results []Result
   status string
}

type Result struct {
   address_components []Address
   formatted_address string
   geometry Geometry
   place_id string
   types []string
}

type Address struct {
   long_name string
   short_name string
   Type []string `json:"type"`
}

type Geometry struct {
   bounds Bounds
   location LatLng
   location_type string
   viewport Bounds
}

type Bounds struct {
   northeast LatLng
   southwest LatLng
}

type LatLng struct {
   lat float64
   lng float64
}

查询结果(带curl)

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Suresnes",
               "short_name" : "Suresnes",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Hauts-de-Seine",
               "short_name" : "Hauts-de-Seine",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Île-de-France",
               "short_name" : "Île-de-France",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "France",
               "short_name" : "FR",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "92150",
               "short_name" : "92150",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "92150 Suresnes, France",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 48.88276,
                  "lng" : 2.2364639
               },
               "southwest" : {
                  "lat" : 48.859284,
                  "lng" : 2.199768
               }
            },
            "location" : {
               "lat" : 48.869798,
               "lng" : 2.219033
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 48.88276,
                  "lng" : 2.2364639
               },
               "southwest" : {
                  "lat" : 48.859284,
                  "lng" : 2.199768
               }
            }
         },
         "place_id" : "ChIJ584OtMVk5kcR4DyLaMOCCwQ",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

查询结果(带我的go代码)

DEBUG: %!(EXTRA controlgoogle.Results={[] }) 

你能帮我把这个查询结果解析成一个结构吗?

谢谢

最佳答案

要将 JSON 解码为结构,它需要访问成员以便更新值。要允许访问,您必须导出结构的成员(通过以大写开头的成员名称)。 JSON 字段也应该使用 json:"<field_name>" 映射到结构的成员。 .我已经更新了你的结构。

type Results struct {
   Results []Result `json:"results"`
   Status string `json:"status"`
}

type Result struct {
   AddressComponents []Address `json:"address_components"`
   FormattedAddress string `json:"formatted_address"`
   Geometry Geometry `json:"geometry"`
   PlaceId string `json:"place_id"`
   Types []string `json:"types"`
}

type Address struct {
   LongName string `json:"long_name"`
   ShortName string `json:"short_name"`
   Types []string `json:"types"`
}

type Geometry struct {
   Bounds Bounds `json:"bounds"`
   Location LatLng `json:"location"`
   LocationType string `json:"location_type"`
   Viewport Bounds `json:"viewport"`
}

type Bounds struct {
   Northeast LatLng `json:"northeast"`
   Southwest LatLng `json:"southwest"`
}

type LatLng struct {
   Lat float64 `json:"lat"`
   Lng float64 `json:"lng"`
}

关于json - 使用 App Engine 在 golang 中将 json 解析为来自 google api 请求的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38515417/

相关文章:

google-app-engine - 如何使用实体键在 GQL 中查询

go - 如何将 sqlx 查询结果转换为结构数组?

javascript - 使用 PHP 数组中的 JSON.parse 时出现“意外标记”

Javascript jquery 帮助 w/for 循环按钮

php - 统一数组进行 JSON 编码?

python - 解析嵌套的Python字典

java - 对于谷歌应用程序引擎: Compare and Contrast Lucene and Search Api

python - 如何对 Google Cloud Endpoints 进行单元测试

go - 从 net.UDPConn 读取会锁定 PC

go - 为什么与 == 相比,具有相同日期和时间的 2 个时间结构返回 false?