json - 解析嵌套的 json 对象 golang

标签 json go unmarshalling

我想解析提到的json

{"foo": [{ "bar": 1, "baz": 2 },{ "bar": 4, "baz": 25 }], "more": "text"}

使用下面的结构解码

type FooStruct struct {
   Bar int `json:"bar"`
   Baz int `json:"baz"`
}

type  ResponseStruct struct {
    More string `json:"more"`
    Foo []FooStruct `json:"foo"`
}
var contentHtml ResponseStruct
err = json.Unmarshal(<byte_array>, &contentHtml)
fmt.Printf("%+v", contentHtml.FooStruct[0].Bar)

最佳答案

你的代码是正确的,除了这一行:

fmt.Printf("%+v", contentHtml.FooStruct[0].Bar)

ResponseStruct 类型中没有字段 FooStruct。我认为您需要改用 Foo
上的工作示例 Go playground

关于json - 解析嵌套的 json 对象 golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32731671/

相关文章:

java - 使用 GSON 比较两个 JSON 字符串的正确方法是什么?

go - reflect.Set slice-of-structs value to a struct,没有类型断言(因为它是未知的)

java - 如何将数据库 XML 输出映射到 JAXB 派生对象模型

ruby - 比 Ruby 编码(marshal)更快/更有效的替代品?

ruby-on-rails - 使用引用对象树查询结果 - Ruby on Rails

java - Android JAVA从php获取数组

google-app-engine - 通过 Cloud Datastore API 连接到开发中的 AppEngine 数据存储

java - Spring with Castor - 初始化应用程序上下文时出现空指针异常

javascript - 如何获取特定年份的 json 数据?

debugging - Go语言中常见的陷阱有哪些?