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

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

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:

  1. {
  2. "response": {
  3. "requests": [
  4. {
  5. "request": {}
  6. },
  7. {
  8. "request": {
  9. "id": 589748,
  10. "image_thumbnail": "",
  11. "description": "Blah blah",
  12. "status": "received",
  13. "user": "test"
  14. }
  15. }
  16. ],
  17. "count": "50",
  18. "benchmark": 0.95516896247864,
  19. "status": {},
  20. "debug": {}
  21. }
  22. }
  23. type Request struct {
  24. id int `json:"id"`
  25. description string `json:"description"`
  26. user string `json:"user"`
  27. }
  28. type Requests struct {
  29. request Request `json:"request"`
  30. }
  31. type Response struct {
  32. requests []Requests `json:"requests"`
  33. }
  34. type RootObject struct {
  35. response Response `json:"response"`
  36. }
  37. url := "<webservice>"
  38. req, err := http.NewRequest("GET", url, nil)
  39. req.Header.Set("Content-Type", "application/json")
  40. client := &http.Client{}
  41. resp, err := client.Do(req)
  42. if err != nil {
  43. panic(err)
  44. }
  45. defer resp.Body.Close()
  46. var r RootObject
  47. decoder := json.NewDecoder(resp.Body)
  48. decoder.Decode(&r)
  49. fmt.Printf("%+v", r)
  50. for _, req := range r.response.requests {
  51. fmt.Printf("%d = %s\n", req.request.id, req.request.user)
  52. }

答案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:

确定