我想解析提到的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/