I want to parse the following JSON output:
{ "total":5689, "result":{ "6581":{ "percent":37.79, "count":2150 }, "6591":{ "percent":35.31, "count":2009 }, "6601":{ "percent":26.89, "count":1530 } } } I have read that JSON can be parsed into a struct if the format is known:
package main import ( "encoding/json" "fmt" "os" ) type VoteResult struct { Total int `json:"total"` Result struct { Efid1 struct { Percent float64 `json:"percent"` Count int `json:"count"` } Efid2 struct { Percent float64 `json:"percent"` Count int `json:"count"` } Efid3 struct { Percent float64 `json:"percent"` Count int `json:"count"` } } } func main() { b := []byte(`{"total":5689,"result":{"6581":{"percent":37.79,"count":2150} ,"6591":{"percent":35.31,"count":2009},"6601":{"percent":26.89,"count":1530}}}`) var v VoteResult err := json.Unmarshal(b, &v) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } fmt.Println(v) } This is the output, but something is wrong as the nested structs are not filled with data:
{5689 {{0 0} {0 0} {0 0}}}
What am I doing wrong?