我正在尝试使用 Flickr API,并且我有一个端点,它给了我这样的响应
{
"photos": {
"page": 1,
"pages": 500,
"perpage": 1,
"total": 500,
"photo": [
{
"id": "12345",
"owner": "12345@N01",
"secret": "12ab345xyz",
"server": "1234",
"farm": 1,
"title": "Some Title",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
...
]
},
"stat": "ok"
}
我有一个看起来像这样的结构
type FlickrResp struct {
Photos struct {
Page int `json:"page"` //what page the results were retreived from
Pages int `json:"pages"` //total number of pages of results
Perpage int `json:"perpage"` //number of results per page
Total int `json:"total"` //total number of results
} `json:"photo"` //array of objects with details of photos
} `json:"photos"` //object that holds information about photoset
Stat string `json:"stat"` //status
}
我正在尝试解组
var fr FlickrResp //create blank struct of type FlickrResp to hold JSON
resp, err := ioutil.ReadAll(flickrCB.Body) //read HTTP GET response
if err != nil {
}
json.Unmarshal([]byte(resp), &fr) //serialize JSON into template
flickCB.Body 来自一个 http.Get
问题是我只得到照片数组的第一个元素。当我打印转换为字符串的响应对象时,我可以看到我有多个元素。我究竟做错了什么?几天来,我一直在反对 Unmarshal 和更复杂的 JSON 响应。
郎朗坤
相关分类