Representing a Golang Structure Given a JSON structure

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

Representing a Golang Structure Given a JSON structure

问题

以下是翻译好的内容:

  1. type requestNodeType struct {
  2. // edited: added the last part
  3. urls []string `json:"urls"`
  4. }
  5. ...一些其他的代码...然后是设置gin路由上下文的部分...
  6. c ->>> *gin.Context
  7. 然后...
  8. x, _ := ioutil.ReadAll(c.Request.Body)
  9. fmt.Printf("crb2 = %s\n", string(x))
  10. uList := requestNodeType{}
  11. json.Unmarshal(x, &uList)
  12. // edited: updated prints for clarity
  13. fmt.Printf("json1 = %+v, %T - %p\n", uList, uList, uList)
  14. fmt.Printf("json2 = %+v, %T - %p\n", uList.urls, uList.urls, uList.urls)
  15. fmt.Printf("json3 = %+v, %T - %p\n", uList.urls[0], uList.urls[0], uList.urls[0])
  16. 输出结果为
  17. crb2 = {"urls":["http://www.indeed.com/viewjob?jk=9388f66529358f6a", "http://www.indeed.com/viewjob?jk=53e937ef73c0c808"]}
  18. json1 = {urls:[]}, main.requestNodeType - %!p(main.requestNodeType={[]})
  19. json2 = [], []string - 0x0
  20. 2016/02/20 09:10:39 Panic recovery -> runtime error: index out of range
  21. 如何正确表示这个结构或修复我的代码
  22. 或者更好的是有没有办法让c.BindJSON(&uList)Gin中工作...
英文:
  1. type requestNodeType struct {
  2. // edited: added the last part
  3. urls []string `json:"urls"`
  4. }

... some more code ... then a part where I set the gin router context ...
c ->>> *gin.Context

and then ...

  1. x, _ := ioutil.ReadAll(c.Request.Body)
  2. fmt.Printf("crb2 = %s\n", string(x))
  3. uList := requestNodeType{}
  4. json.Unmarshal(x, &uList)
  5. // edited: updated prints for clarity
  6. fmt.Printf("json1 = %+v, %T - %p\n", uList, uList, uList )
  7. fmt.Printf("json2 = %+v, %T - %p\n", uList.urls, uList.urls, uList.urls )
  8. fmt.Printf("json3 = %+v, %T - %p\n", uList.urls[0], uList.urls[0], uList.urls[0] )

gives me an output of:

  1. crb2 = {"urls":["http://www.indeed.com/viewjob?jk=9388f66529358f6a", "http://www.indeed.com/viewjob?jk=53e937ef73c0c808"]}
  2. json1 = {urls:[]}, main.requestNodeType - %!p(main.requestNodeType={[]})
  3. json2 = [], []string - 0x0
  4. 2016/02/20 09:10:39 Panic recovery -> runtime error: index out of range

How can I represent this structure properly or fix my code?

Or even better, an idea to get c.BindJSON(&uList) to work for Gin ...?

答案1

得分: 1

你的JSON无效,如果它确实等于

  1. {"http://www.jobs.com/job?jk=9388f66529358f6a","http://www.job.com/job?jk=53e937ef73c0c808"}

所以你的uList.URIs是空的。你可以检查解析错误json.Unmarshal的结果。

适合你的模型的正确JSON应该是

  1. ["http://www.jobs.com/job?jk=9388f66529358f6a","http://www.job.com/job?jk=53e937ef73c0c808"]

结构应该是

  1. type URLs []string

或者

  1. {"URLs": ["http://www.jobs.com/job?jk=9388f66529358f6a","http://www.job.com/job?jk=53e937ef73c0c808"]}

结构应该是

  1. type requestNodeType struct {
  2. URLs []string `json:"URLs"`
  3. }

另外,你可以将json.Unmarshal([]byte(string(x)), &uList)替换为json.Unmarshal(x, &uList),因为x已经是[]byte类型了。

英文:

Your JSON is invalid if it is really equal to

  1. {["http://www.jobs.com/job?jk=9388f66529358f6a","http://www.job.com/job?jk=53e937ef73c0c808"]}

So your uList.URIs is empty. You could check for parsing error json.Unmarshal result.

Proper JSON for your model should look like

  1. ["http://www.jobs.com/job?jk=9388f66529358f6a","http://www.job.com/job?jk=53e937ef73c0c808"]

with struct like

  1. type URLs []string

or

  1. {"URLs": ["http://www.jobs.com/job?jk=9388f66529358f6a","http://www.job.com/job?jk=53e937ef73c0c808"]}

with struct like

  1. type requestNodeType struct {
  2. URLs []string `json:"URLs"`
  3. }

Also you can replace json.Unmarshal([]byte(string(x)), &uList) with json.Unmarshal(x, &uList) as x is already []byte.

huangapple
  • 本文由 发表于 2016年2月20日 17:28:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/35521356.html
匿名

发表评论

匿名网友

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

确定