Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshal json into struct: cannot unmarshal array into Go value

I have a service which provides me properties through REST. Now I want to unmarshal the body into a properties struct. Please see this playground example: click. When I have only one property, I can easily unmarshal it into a Property. However the ACTUAL response from the server is somehow difference. The actual response I want to unmarshal is this:

[
    {
        "key": "blabla",
        "secret": false,
        "type": "string",
        "value": "hereisthevalue"
    },
    {
        "key": "yepyepakey",
        "secret": true,
        "type": "string",
        "value": "dummy"
    }
]

Unfortunately I don't know how to unmarshal this. Can someone please point me in the right direction?

like image 763
Rogier Lommers Avatar asked Dec 11 '15 14:12

Rogier Lommers


1 Answers

You need to unmarshal into a slice of Property: http://play.golang.org/p/eRgjfBHypH

var props []Property
er := json.Unmarshal(resp, &props)
if er != nil {
    panic(er)
} else {
    fmt.Println(props)
}
like image 164
JimB Avatar answered Oct 22 '22 21:10

JimB