mercredi 9 septembre 2020

Golang can't decode a struct (invalid character '[' looking for beginning of object key string)

I'm trying to send this mock data that i prepared for testing one my functions. It fails by saying 'invalid character '[' looking for beginning of object key string'. It is not able to decode the

type Payload struct {
     Team []map[string]interface{} `json:"teams"`
     Players []map[string]interface{} `json:"players"`
     Time      map[string]interface{}   `json:"time"`
}

var Players Payload


body:= strings.NewReader(fmt.Sprintf("%v", Payload{
            Team: []map[string]interface{}{
                {
                    "location":"LA",
                    "number_of_members":"15992",
                    "name":"Los Angeles Lakers",
                    "rank":3
                },
            },
            Players: []map[string]interface{}{
                {
                    "name":"Kuzma"
                    "championship":1
                    "stats": map[string]interface{}{
                        "pts":   20,
                        "assists": 12,
                    },
                },
            },
            Time: map[string]interface{}{
                "endDate":          endDate,
                "isUpdateLineItem": isUpdateLineItem,
                "startDate":        startDate,
                "timezone":         timezone,
            },
        },
))


if err := json.NewDecoder(body).Decode(&Players); err != nil {
    errors.Write(rw, 400, "Unable to parse request body JSON or invalid data format.")
    return
}

Aucun commentaire:

Enregistrer un commentaire