I get an response body. the format of the body is as below:
[
{
"id":1,
"name":"111"
},
{
"id":2,
"name":"222"
}
]
I want to parse the body to json struct, my code is as below:
type Info struct {
Id uint32 `json:id`
Name string `json:name`
}
type Response struct {
infos []Info
}
v := &Response{}
data, err := ioutil.ReadAll(response.Body)
if err := json.Unmarshal(data, v); err != nil {
log.Fatalf("Parse response failed, reason: %v \n", err)
}
It always give an error:cannot unmarshal array into Go value of type xxx, can someone give a help?
json.Unmarshal(data, &v.infos). If json is an array, you need to pass in a Go slice, if it's an object you need to pass in a Gostruct, ormap.