对于我的一个项目,我需要像下面这样编码成 Json。我将所有值都作为变量。感谢您的帮助。
{"id":[{"name":"Test","Class":[{"Grade":"2","id":"34"}]}],"age":"5 "
这是我试过的代码
type classx struct {
Grade string `json:"grade"`
Id string `json:"id"`
}
type idx struct {
Name string `json:"name"`
Class []classx
}
type Response struct {
Age string `json:"age"`
Id []idx
}
但是出现错误“不能在字段值中使用 classx 文字(类型 classx)作为类型 []classx”
最佳答案
类字段是一个 slice 。你给了它一个结构
错误:
Response{Id:[]idx{idx{Class:classx{}}}}
正确:
Response{Id:[]idx{idx{Class:[]classx{}}}}
关于json - Go 嵌套 Json Marshall 或编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38268859/