This question already has an answer here:
I receive dynamic JSON from an API that follows this pattern:
{
"ts": timestamp,
"data": [
[ code1, payload1 ],
[ code2, payload2 ],
...
]
}
For example the raw data will be:
var streamSnapshot = []byte(
`{
"ts": 1496244373.04,
"data":[
["xrate", {"rate":1.2916,"ccy":"USD"}],
["balance",
{
"open_stake":["GBP", 0.0],
"balance":["GBP", 0.0]
}
],
["event",
{
"competition_id":"545",
"ir_status":"pre_event",
"start_time":"2017-09-10T17:00:00+00:00",
"competition_name":"USA NFL",
"event_id":"2017-09-10,21617,21635",
"home":"Buffalo Bills",
"away":"New York Jets",
"sport":"af",
"competition_country":"US"
}
],
["sync", {"Token":"eb1c57132d004f8d8fb967c076921fac"}]
]
}`)
Considering that we want to avoid unmarshalling to a struct like this:
type StreamMessage struct {
Data [][]interface{} `json:"data"`
Ts float64 `json:"ts"`
}
where we would have to cast the data back like:
m := raw.(map[string]interface{})
switch messageType {
case XRATE:
xrate := XRate{
Message: Message{
Type: XRATE,
TimeStamp: msg.Ts,
},
rate: m["rate"].(float64),
ccy: m["ccy"].(string),
}
...
What would be the better way of unmarshalling this?
</div>