Reputation: 3754
I have a JSON object similar to this one:
{
"prices": {
"7fb832f4-8041-4fe7-95e4-6453aeeafc93": {
"diesel": 1.234,
"e10": 1.234,
"e5": 1.234,
"status": "open"
},
"92f703e8-0b3c-46da-9948-25cb1a6a1514": {
"diesel": 1.234,
"e10": 1.234,
"e5": 1.234,
"status": "open"
}
}
I am not sure how to unmarshal this into an GO object without losing the unique ID field of each sub-item which is important information for me.
Upvotes: 0
Views: 120
Reputation: 120951
Use a map:
type Station struct {
Diesel float64
E10 float64
E15 float64
Status string
}
type Data struct {
Prices map[string]*Station
}
Upvotes: 2
Reputation:
You can use a map
with string keys to preserve the unique IDs of each sub-price:
type Object struct {
Prices map[string]*Price `json:"prices"`
}
type Price struct {
Diesel float32 `json:"diesel"`
E10 float32 `json:"e10"`
E5 float32 `json:"e5"`
Status string `json:"status"`
}
Then, for example, you could loop over the unmarshaled object:
for id, price := range o.Prices {
fmt.Printf("%s %v\n", id, price)
}
https://play.golang.org/p/aPhvGdtFC_
Upvotes: 7