I am running a query on a database that I have on a sever. The problem is when I am trying to decode the JSON into a 2D slice I get an error because one of them elements is a string and the other element is a float64.
One way I tried to resolve this issue is my modifying the JSON string before I decode it by adding quotes to make the number a string. But is there a better way of doing this? Is there a way I can modify the struct of my code where I am able to decode the JSON code?
2018/05/04 12:32:19 json: cannot unmarshal number into Go struct field .values of type string
import (
"fmt"
"encoding/json"
"strings"
"io"
"log"
)
func main(){
str := "{\"results\":[{\"statement_id\":0,\"series\":[{\"name\":\"_\",\"columns\":[\"time\",\"last\"],\"values\":[[\"2018-03-20T18:45:57.07Z\",142774272]]}]}]}"
type Response struct {
Results []struct {
StatementID int `json:"statement_id"`
Series []struct {
Name string `json:"name"`
Columns []string `json:"columns"`
Values [][]string `json:"values"`
} `json:"series"`
} `json:"results"`
}
dec := json.NewDecoder(strings.NewReader(str))
for {
var m Response
if err := dec.Decode(&m); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
fmt.Println(m.Results[0].Series[0].Values[0])
}
}