2017-04-11 2 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" 
      } 
     ] 
    } 
} 

나는 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) 

} 

답변

2

Go 유형이 JSON의 구조와 일치하지 않습니다. JSON에는 한 가지 수준의 객체가 있습니다. 사용해보기 :

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

일부 문자열 필드는 JSON의 숫자에 해당합니다. 이 필드를 숫자 형 (int, float64, ...)으로 선언하십시오.

playground example

0

당신의 구조체는 사용자가 제공 JSON으로 일치하지 않는 IMHO. 시도 :

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) (와 결과 이동 GET -u github.com/davecgh/go-를 알아보기 힘들 분출/분출) :

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" 
    } 
    } 
} 
}) 
관련 문제