2017-04-11 92 views
0

你好我嘗試解組api.nal.usda.gov/ndb到結構中的JSON響應,但它總是返回空:Golang和解組JSON從api.nal.usda.gov/ndb

{  []} 

示例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" 
      } 
     ] 
    } 
} 

我檢查了JSON響應https://jsonformatter.curiousconcept.com/,並沒有問題。我希望你能告訴我爲什麼,因爲我對Golang來說很新。

我的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 
} 

代碼:

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

Go類型與JSON的結構不匹配。 JSON中還有一層對象。試試這個:

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

某些字符串字段對應於JSON中的數字。用數值類型(int,float64,...)聲明這些字段。

playground example

0

恕我直言,你的結構不匹配成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) 
} 

編輯:這是難以閱讀的println或用的printf%的結果+ V所以這裏是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" 
    } 
    } 
} 
})