dragonsun2005 2017-04-11 15:02
浏览 39
已采纳

Golang从api.nal.usda.gov/ndb解组JSON

Hello I try to unmarshal the JSON response from api.nal.usda.gov/ndb into struct, but it always returns empty:

{        []}

Example 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"
            }
        ]
    }
}

I checked the JSON response with https://jsonformatter.curiousconcept.com/, and it´s fine. I hope you can tell me why, since I am quite new to Golang.

My Structs:

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
}

Code:

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)

}
  • 写回答

2条回答 默认 最新

  • duanlun1955 2017-04-11 15:30
    关注

    The Go types do not match the structure of the JSON. There's one more level of objects in the JSON. Try this:

    var v struct {
        List List
    }
    err := json.Unmarshal([]byte(data), &v)
    

    Some of the string fields correspond to numbers in the JSON. Declare these fields with a numerical type (int, float64, ...).

    playground example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?