在Go中,为什么我的JSON解码不能在这里工作?

Ste*_*sky 2 json decoding go

我无法让标准库的encoding/json包用于解码JSON对象.这是一个最小的例子:

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)
Run Code Online (Sandbox Code Playgroud)

这打印:

contents = {groups:[]}
err = nil
Run Code Online (Sandbox Code Playgroud)

但我希望:

contents = {groups:[{name:foo} {name:bar}]}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Cal*_*leb 6

字段名称必须以大写字母开头:

type Group struct{ Name string }
var contents struct {
    Groups []Group
}
Run Code Online (Sandbox Code Playgroud)