iancoleman / orderedmap

orderedmap is a golang map where the keys keep the order that they're added. It can be de/serialized from/to JSON. It's based closely on the python collections.OrderedDict.
MIT License
356 stars 55 forks source link

how to unmarshal json array #31

Closed AlpJQ closed 1 year ago

AlpJQ commented 2 years ago
s := `[{"a":1, "b":2},{"a":0, "b":3},]`
o := orderedmap.New()
err := json.Unmarshal([]byte(s), &o)

however, it does not work

gkampitakis commented 1 year ago

Your provided input is not a valid json

- `[{"a":1, "b":2},{"a":0, "b":3},]`
+ `[{"a":1, "b":2},{"a":0, "b":3}]`
iancoleman commented 1 year ago

OrderedMap will unmarshal the same way a map does. In this case @gkampitakis is correct, the error would be invalid character ']' looking for beginning of value. To reproduce:

b := []byte(`[{"a":1, "b":2},{"a":0, "b":3},]`)
m := []map[string]int{}
err := json.Unmarshal(b, &m)

The error for unmarshal to map is the same as the error for OrderedMap, which is expected.