英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论