json - Go 嵌套 Json Marshall 或编码

标签 json go encoding marshalling

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

相关文章:

php - 正则表达式不起作用

c# - 如何将整数数组的字符串拆分为 List<int[]>?

javascript - 将渲染的 Django 模板与其他一些项目一起放入 Json 中

go - 了解 golang 中的 mux 路由器

go - 短变量声明和 "variable declared and not used"错误

java - ISO 8859-1 Java 程序打印文件的编码

android - 将数据添加到ArrayList android

c# - System.Runtime.Serialization.Json 命名空间在哪里?

security - 使用Go linter遇到安全问题

php - 我有一个 UTF-8 mysql 数据库中的 latin1 编码数据,我该如何解决这个问题?