[英]parse json to struct from google api request in golang with App Engine
我實際上正在嘗試在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構造,它需要訪問成員,以便可以更新值。 要允許訪問,您必須導出struct的成員(通過以大寫字母開頭成員名稱)。 JSON字段也應使用json:"<field_name>"
映射到struct的成員。 我已經更新了您的結構。
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"`
}
注意結構的字段情況。 如果我沒記錯的話,marshaller / unmarshaller將忽略小寫字段,因此您最終將得到空結構。 嘗試將字段命名為大寫。
您還可以使用第三方服務(例如Serp API) ,該服務是已經提供JSON解析的Google搜索引擎結果API。
與GoLang集成很容易:
parameter := map[string]string{
"q": "Coffee",
"location": "Portland"
}
query := newGoogleSearch(parameter)
serpResponse, err := query.json()
results := serpResponse["organic_results"].([]interface{})
first_result := results[0].(map[string]interface{})
fmt.Println(ref["title"].(string))
GitHub: https : //github.com/serpapi/google-search-results-golang
具有json標簽的結構字段需要導出(大寫字段名稱)。 另外,您應該為所有struct字段添加json標記,json標記必須與要解析的json字段相同:
type Address struct {
LongName string `json:"long_name"`
ShortName string `json:"short_name"`
Type []string `json:"types"`
}
a := `{
"long_name" : "Suresnes",
"short_name" : "Suresnes",
"types" : [ "locality", "political" ]
}`
var addr Address
err := json.NewDecoder(strings.NewReader(a)).Decode(&addr)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%+v\n", addr)
output: {LongName:Suresnes ShortName:Suresnes Type:[locality political]}
默認情況下,encoding / json包僅封送/解組僅輸出字段,因此您應將結構更改為以下形式,以大寫開頭並具有json:“”標簽。
type Results struct {
Results []Result `json:"results"`
Status string `json:"status"`
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.