嵌套 Json Marshall 或编码

Go Nested Json Marshall or Encoding

对于我的一个项目,我需要像下面这样编码成 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 
} 

但是出现错误"cannot use classx literal (type classx) as type []classx in field value"

Class 字段是一个切片。你给了它一个结构

错误:

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

正确:

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