简体   繁体   English

嵌套Json Marshall或编码

[英]Go Nested Json Marshall or Encoding

For a project of mine, i need to encode into Json like below. 对于我的项目,我需要像下面这样编码到Json中。 I have all the values as variables. 我将所有值都作为变量。 Any help is appreciated. 任何帮助表示赞赏。

{"id":[{"name":"Test","Class":[{"Grade":"2","id":"34"}]}],"age":"5"} {“ id”:[{“ name”:“ Test”,“ Class”:[{“ Grade”:“ 2”,“ id”:“ 34”}]}]],“ age”:“ 5”}

this is the code i have tried 这是我尝试过的代码

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 
} 

But getting an error "cannot use classx literal (type classx) as type []classx in field value" 但是出现错误“无法在字段值中使用classx文字(typex类型)作为[] classx类型”

The Class field is a slice. “类别”字段是一个切片。 You have given it a struct 你给了它一个结构

Wrong: 错误:

Response{Id:[]idx{idx{Class:classx{}}}}

Correct: 正确:

Response{Id:[]idx{idx{Class:[]classx{}}}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM