动态数组json解析

I am trying to parse json to go lang struck but some how the object is return empty:

Json Object:

`{
    "names": [
        {
            "David": {
                "id": "100",
                "country": "usa",
                "group": [
                    "A1",
                    "A2"
                ]
            }
        },
        {
            "John": {
                "id": "1",
                "country": "uk",
                "group": [
                    "A1",
                    "A2"
                ]
            }
        }
    ]
}`

GoLang struct:

type Data struct {
    Names []Names `json:"names"`
}

type Names struct {
    ID      string   `json:"id"`
    Country string   `json:"country"`
    Group   []string `json:"group"`
}

The issue that array contains 2 element David,John somehow it return empty object as the parser has problem to extract the strings David,John

The struct Data's Names field is wrong. It is a slice of map[string]Names rather []Names. So just change it to:

Names []map[string]Names `json:"names"`

Check the full runnable code at https://play.golang.org/p/IDU0jANRbBn.

Here you have another idea:

Parsing into a map

type Dictionary map[string]json.RawMessage

// Generate a Diccionary from JSON body
func Generate(body io.Reader) (Dictionary, error) {
    decoder := json.NewDecoder(body)
    err := decoder.Decode(&d)
    return d, err
}

Use:

d, err = Generate(strings.NewReader(string(jsonHere)))