[英]Golang unmarshall JSON from api.nal.usda.gov/ndb
您好,我嘗試將api.nal.usda.gov/ndb中的JSON響應解組到struct中,但它始終返回空:
{ []}
JSON示例:
{
"list": {
"q": "butter",
"sr": "28",
"ds": "any",
"start": 0,
"end": 50,
"total": 4003,
"group": "",
"sort": "r",
"item": [
{
"offset": 0,
"group": "Branded Food Products Database",
"name": "BLUE BUNNY, PEANUT BUTTER PANIC, ICE CREAM, POWERFUL PEANUT BUTTER ICE CREAM CHARGED WITH PEANUT BUTTER AND FUDGE SAUCES AND OVERLOADED WITH PEANUT BUTTER CUPS, UPC: 070640034086",
"ndbno": "45011419",
"ds": "BL"
},
{
"offset": 1,
"group": "Branded Food Products Database",
"name": "BLUE BUNNY, ICE CREAM, PEANUT BUTTER PARTY, PEANUT BUTTER ICE CREAM, PEANUT BUTTER AND FUDGE SWIRLS, PEANUT BUTTER CUPS, UPC: 070640012411",
"ndbno": "45110466",
"ds": "BL"
}
]
}
}
我使用https://jsonformatter.curiousconcept.com/檢查了JSON響應,這很好。 我希望您能告訴我為什么,因為我剛接觸Golang。
我的結構:
type List struct {
Q string `json:"q,omitempty"`
Sr string `json:"sr,omitempty"`
Ds string `json:"ds,omitempty"`
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
Total string `json:"total,omitempty"`
Group string `json:"group,omitempty"`
Sort string `json:"sort,omitempty"`
Item []Item `json:"item,omitempty"`
}
type Item struct {
Offset string `json:"offset,omitempty"`
Group string `json:"group,omitempty"` //food group to which the food belongs
Name string `json:"name,omitempty"` //the food’s name
Ndbno string `json:"nbno,omitempty"` //the food’s NDB Number
Ds string `json:"ds,omitempty"` //Data source: BL=Branded Food Products or SR=Standard Release
}
碼:
func (sr *SearchRequest) fetch() {
url := "https://api.nal.usda.gov/ndb/search/?" +
"format=" + sr.format +
"&q=" + sr.q +
"&sort=" + sr.sort +
"&max=" + strconv.FormatInt(sr.max, 10) +
"&offset=" + strconv.FormatInt(sr.offset, 10) +
"&api_key=" + sr.c.ApiKey
r, err := http.Get(url)
if err != nil {
panic(err.Error())
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err.Error())
}
l := new(List)
err = json.Unmarshal(b, &l)
if err != nil {
fmt.Println(err)
}
fmt.Println(l)
}
Go類型與JSON的結構不匹配。 JSON中還有一層對象。 嘗試這個:
var v struct {
List List
}
err := json.Unmarshal([]byte(data), &v)
一些字符串字段對應於JSON中的數字。 用數字類型(int,float64等)聲明這些字段。
恕我直言,您的結構與您提供的JSON不匹配。 嘗試:
package main
import (
"encoding/json"
"fmt"
)
type MyItem struct {
Q string `json:"q,omitempty"`
Sr string `json:"sr,omitempty"`
Ds string `json:"ds,omitempty"`
Start int `json:"start,omitempty"`
End int `json:"end,omitempty"`
Total int `json:"total,omitempty"`
Group string `json:"group,omitempty"`
Sort string `json:"sort,omitempty"`
Item []Item `json:"item,omitempty"`
}
type MyList struct {
List MyItem `json:"list"`
}
type Item struct {
Offset int `json:"offset,omitempty"`
Group string `json:"group,omitempty"` //food group to which the food belongs
Name string `json:"name,omitempty"` //the food’s name
Ndbno string `json:"nbno,omitempty"` //the food’s NDB Number
Ds string `json:"ds,omitempty"` //Data source: BL=Branded Food Products or SR=Standard Release
}
var jsonStr = `{
"list": {
"q": "butter",
"sr": "28",
"ds": "any",
"start": 0,
"end": 50,
"total": 4003,
"group": "",
"sort": "r",
"item": [
{
"offset": 0,
"group": "Branded Food Products Database",
"name": "BLUE BUNNY, PEANUT BUTTER PANIC, ICE CREAM, POWERFUL PEANUT BUTTER ICE CREAM CHARGED WITH PEANUT BUTTER AND FUDGE SAUCES AND OVERLOADED WITH PEANUT BUTTER CUPS, UPC: 070640034086",
"ndbno": "45011419",
"ds": "BL"
},
{
"offset": 1,
"group": "Branded Food Products Database",
"name": "BLUE BUNNY, ICE CREAM, PEANUT BUTTER PARTY, PEANUT BUTTER ICE CREAM, PEANUT BUTTER AND FUDGE SWIRLS, PEANUT BUTTER CUPS, UPC: 070640012411",
"ndbno": "45110466",
"ds": "BL"
}]
}
}`
func main() {
b := jsonStr
l := new(MyList)
err := json.Unmarshal([]byte(b), &l)
if err != nil {
fmt.Println(err)
}
fmt.Println(l)
}
編輯:很難用%+ v讀取println或printf的結果,所以這里是spew.Dump(l)的結果(去-u github.com/davecgh/go-spew/spew):
List: (main.MyItem) {
Q: (string) (len=6) "butter",
Sr: (string) (len=2) "28",
Ds: (string) (len=3) "any",
Start: (int) 0,
End: (int) 50,
Total: (int) 4003,
Group: (string) "",
Sort: (string) (len=1) "r",
Item: ([]main.Item) (len=2 cap=4) {
(main.Item) {
Offset: (int) 0,
Group: (string) (len=30) "Branded Food Products Database",
Name: (string) (len=178) "BLUE BUNNY, PEANUT BUTTER PANIC, ICE CREAM, POWERFUL PEANUT BUTTER ICE CREAM CHARGED WITH PEANUT BUTTER AND FUDGE SAUCES AND OVERLOADED WITH PEANUT BUTTER CUPS, UPC: 070640034086",
Ndbno: (string) "",
Ds: (string) (len=2) "BL"
},
(main.Item) {
Offset: (int) 1,
Group: (string) (len=30) "Branded Food Products Database",
Name: (string) (len=138) "BLUE BUNNY, ICE CREAM, PEANUT BUTTER PARTY, PEANUT BUTTER ICE CREAM, PEANUT BUTTER AND FUDGE SWIRLS, PEANUT BUTTER CUPS, UPC: 070640012411",
Ndbno: (string) "",
Ds: (string) (len=2) "BL"
}
}
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.