I'm a beginner in Go and I'm trying to call a json rest-API for which I'm trying to use the goreq request lib. In the readme it gives the following example for decoding the received json:
type Item struct {
Id int
Name string
}
var item Item
res.Body.FromJsonTo(&item)
I understand this example, but the json I'm receiving is way more complex (see below). I'm not sure how I would create a struct
which represents this complex structure. Can I write it in one struct
, or do I need to use maps for the bid and ask arrays, another struct for the bid and ask objects, and yet one more struct for the "vars"
object?
Isn't there any automagic json_to_struct()
function which dynamically builds the struct upon receiving the json (I've looked but I can't find anything)?
All tips are welcome!
{
"success": true,
"message": "something",
"vars": {"some": "value", "thenumber": 7612.32},
"result": {
"bids": [
{"quantity": 2, "price": 416.02, "cm": "some text"},
etc..
],
"asks": [
{"quantity": 1, "price": 420.02, "cm": "some text"},
etc..
],
"slip": 4
}
}