遇到了解码 JSON 响应的问题。

huangapple go评论82阅读模式
英文:

Having trouble decoding JSON response

问题

这是我在Go语言中的第一次尝试,我觉得我在这里漏掉了一些重要的东西。我试图从一个Web服务解码一个JSON消息,但我得到的输出是:

{response:{requests:[]}}

我真正感兴趣的只是请求节点中的数据。我的for循环显然没有被调用,因为数组是空的。我觉得我的结构体需要按照它们在Web服务中的出现顺序进行声明?

示例JSON:

{
"response": {
"requests": [
{
"request": {}
},
{
"request": {
"id": 589748,
"image_thumbnail": "",
"description": "Blah blah",
"status": "received",
"user": "test"
}
}
],
"count": "50",
"benchmark": 0.95516896247864,
"status": {},
"debug": {}
}
}

type Request struct {
id int json:"id"
description string json:"description"
user string json:"user"
}

type Requests struct {
request Request json:"request"
}

type Response struct {
requests []Requests json:"requests"
}

type RootObject struct {
response Response json:"response"
}

url := ""

req, err := http.NewRequest("GET", url, nil)
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}

defer resp.Body.Close()

var r RootObject

decoder := json.NewDecoder(resp.Body)
decoder.Decode(&r)

fmt.Printf("%+v", r)

for _, req := range r.response.requests {
fmt.Printf("%d = %s\n", req.request.id, req.request.user)
}

英文:

This is my first attempt at Go and I feel I'm missing something important here. Trying to decode a JSON message from a webservice but the output I'm getting is:

{response:{requests:[]}}

All I'm really interested in is the data within the request node. My for-loop obviously isn't getting called because the array is empty. I feel like my structs need to be declared exactly as they appear in the webservice?

Sample JSON:

{
"response": {
"requests": [
{
"request": {}
},
{
"request": {
  "id": 589748,
  "image_thumbnail": "",
  "description": "Blah blah",
  "status": "received",
  "user": "test"
 }
}
],
"count": "50",
"benchmark": 0.95516896247864,
"status": {},
"debug": {}
}
}


type Request struct {
 	id           int         `json:"id"`
  	description  string      `json:"description"`
 	user         string      `json:"user"`
}

type Requests struct {
 	request	     Request     `json:"request"`
}

type Response struct {
    requests     []Requests  `json:"requests"`
 }

 type RootObject struct {
	response     Response    `json:"response"`  
}

url := "<webservice>"

req, err := http.NewRequest("GET", url, nil)
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
	panic(err)
}

defer resp.Body.Close()

var r RootObject

decoder := json.NewDecoder(resp.Body)
decoder.Decode(&r)


fmt.Printf("%+v", r)

for _, req := range r.response.requests {
	fmt.Printf("%d = %s\n", req.request.id, req.request.user)

}

答案1

得分: 2

字段名称需要以大写字母开头才能导出为标识符。

英文:

Field names need to begin with upper case character to be exported identifiers.

huangapple
  • 本文由 发表于 2015年8月13日 19:32:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/31987018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定