Representing a Golang Structure Given a JSON structure

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

Representing a Golang Structure Given a JSON structure

问题

以下是翻译好的内容:

type requestNodeType struct {
	// edited: added the last part
	urls []string `json:"urls"`
}

...一些其他的代码...然后是设置gin路由上下文的部分...
c ->>> *gin.Context

然后...

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

输出结果为

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

如何正确表示这个结构或修复我的代码

或者更好的是有没有办法让c.BindJSON(&uList)在Gin中工作...
英文:
type requestNodeType struct {
// edited: added the last part
	urls 	[]string  `json:"urls"` 
}

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

and then ...

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

gives me an output of:

crb2 = {"urls":["http://www.indeed.com/viewjob?jk=9388f66529358f6a", "http://www.indeed.com/viewjob?jk=53e937ef73c0c808"]}
json1 = {urls:[]}, main.requestNodeType - %!p(main.requestNodeType={[]})
json2 = [], []string - 0x0
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无效,如果它确实等于

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

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

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

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

结构应该是

type URLs []string

或者

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

结构应该是

type requestNodeType struct {
	URLs []string `json:"URLs"`
}

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

英文:

Your JSON is invalid if it is really equal to

{["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

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

with struct like

type URLs []string

or

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

with struct like

type requestNodeType struct {
	URLs []string `json:"URLs"`
}

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:

确定