我正在嘗試與JSON API進(jìn)行交互。它有兩個(gè)端點(diǎn):GetTravelTimeAsJSON-指定一個(gè)旅行時(shí)間ID,它返回一個(gè)旅行時(shí)間GetTravelTimesAsJSON-返回一個(gè)包含以上所有TravelTimes的數(shù)組。所以我有一個(gè)像這樣的結(jié)構(gòu):type TravelTime struct { AverageTime int `json:"AverageTime"` CurrentTime int `json:"CurrentTime"` Description string `json:"Description"` Distance float64 `json:"Distance"` EndPoint struct { Description string `json:"Description"` Direction string `json:"Direction"` Latitude float64 `json:"Latitude"` Longitude float64 `json:"Longitude"` MilePost float64 `json:"MilePost"` RoadName string `json:"RoadName"` } `json:"EndPoint"` Name string `json:"Name"` StartPoint struct { Description string `json:"Description"` Direction string `json:"Direction"` Latitude float64 `json:"Latitude"` Longitude float64 `json:"Longitude"` MilePost float64 `json:"MilePost"` RoadName string `json:"RoadName"` } `json:"StartPoint"` TimeUpdated string `json:"TimeUpdated"` TravelTimeID int `json:"TravelTimeID"`}如果在一次旅行中這樣調(diào)用API,我將得到一個(gè)填充的結(jié)構(gòu)(我正在使用此req lib)header := req.Header{ "Accept": "application/json", "Accept-Encoding": "gzip", } r, _ := req.Get("http://www.wsdot.com/Traffic/api/TravelTimes/TravelTimesREST.svc/GetTravelTimeAsJson?AccessCode=<redacted>&TravelTimeID=403", header) var foo TravelTime r.ToJSON(&foo) dump.Dump(foo)如果我轉(zhuǎn)儲(chǔ)響應(yīng),它看起來像這樣:TravelTime { AverageTime: 14 (int), CurrentTime: 14 (int), Description: "SB I-5 Pierce King County Line To SR 512", Distance: 12.06 (float64), EndPoint: { Description: "I-5 @ SR 512 in Lakewood", Direction: "S", Latitude: 47.16158351 (float64), Longitude: -122.481133 (float64), MilePost: 127.35 (float64), RoadName: "I-5" }, JSON在這里:https : //gist.github.com/jaxxstorm/0ab818b300f65cf3a46cc01dbc35bf60如果我將原始TravelTime結(jié)構(gòu)修改為像這樣的切片,它將起作用:type TravelTimes []struct {}但是它不能作為一個(gè)單獨(dú)的響應(yīng)。我以前已經(jīng)做到了,但是由于某種原因,這種失敗使我的大腦無法正常工作。任何幫助表示贊賞。
與Golang中的結(jié)構(gòu)體數(shù)組混淆
慕碼人8056858
2021-04-02 13:15:00