我实际上试图在golang中使用谷歌地图API(在使用urlfetch与应用程序引擎,当我执行一个查询,我无法得到结果的结构。解析json结构从谷歌api请求与应用引擎的golang
我的代码
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
}
查询结果(卷曲)
{
"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"
}
查询结果(跟我走代码)
DEBUG: %!(EXTRA controlgoogle.Results={[] })
你能帮我解析一下这个查询结果吗?
感谢的
它将帮助您的调试注释掉除'status'字段以外的所有内容,unmarshaller将高兴地丢弃未知字段,因此它是一个更简单的测试用例。您还可以使用['ioutil.ReadAll'](https://godoc.org/io/ioutil#example-ReadAll)记录请求正文。你可能也想尝试['http.Get()'](https://golang.org/pkg/net/http/#Get) – Plato
我使用谷歌appengine,它不可能使用http 。获得 – Fantasim