I want to parse mentioned json.
{"foo": [{ "bar": 1, "baz": 2 },{ "bar": 4, "baz": 25 }], "more": "text"}
using below struct to unmarshal
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)
Your code is corrent, except this line:
fmt.Printf("%+v", contentHtml.FooStruct[0].Bar)
There is no fields FooStruct
in ResponseStruct
type. I think you need to use Foo
instead.
Working example on the Go playground