I cannot get the standard library's encoding/json
package to work for decoding JSON objects. Here's a minimal example:
b := []byte(`{"groups":[{"name":"foo"},{"name":"bar"}]}`)
type Group struct{ name string }
var contents struct {
groups []Group
}
err := json.Unmarshal(b, &contents)
fmt.Printf("contents = %+v\nerr = %+v\n", contents, err)
This prints:
contents = {groups:[]}
err = nil
But I expect:
contents = {groups:[{name:foo} {name:bar}]}
What am I doing wrong?
The field names have to start with a capital letter:
type Group struct{ Name string }
var contents struct {
Groups []Group
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With