Re: [go-nuts] Cannot unmarshal array into Go value...

1,060 views
Skip to first unread message

Jeff Hodges

unread,
Sep 20, 2012, 4:45:56 AM9/20/12
to Nick, golan...@googlegroups.com
Looks like its the trailing "," in the inner-most map that's the
problem (that is, `"value": 1,` and `"value" : 2,`). The parser could
probably handle this case better and if you could make an issue, that would
be nice.

I'm not sure how you're generating this json, but, if you have control
over it, consider clearing up those trailing "," as they are invalid
json.

On Wed, Sep 19, 2012 at 10:28 PM, Nick <heb...@gmail.com> wrote:
>
> { "response": { "values": { "1": { "1": [ { "value": 1, } ], "2": [ { "value": 2, } ] }, [...] } } }

Nick

unread,
Sep 20, 2012, 7:43:27 AM9/20/12
to golan...@googlegroups.com, Nick
Thanks for your help, but the in the real JSON I'm parsing unfortunately there are no trailing ",". I just forgot to remove them.

Jesse McNelis

unread,
Sep 20, 2012, 10:03:18 AM9/20/12
to Nick, golan...@googlegroups.com
On Thu, Sep 20, 2012 at 3:28 PM, Nick <heb...@gmail.com> wrote:
> I have got some problems deserializing JSON. So my JSON looks like this:
>
> {
> "response": {
> "values": {
> "1": {
> "1": [
> {
> "value": 1,
> }
> ],
> "2": [
> {
> "value": 2,
> }
> ]
> },
> [...]
> }
> }
> }

The json isn't valid. There shouldn't be trailing commas.

> How would you deserialize that in Go? I always get problems with the array
> containing the object: "Cannot unmarshal array into Go value of type..."
It's a good idea to provide the code that you've written so we can
point out where the problem is.
Here is an example of unmarshaling your json.
http://play.golang.org/p/XXm-jRnzRJ



--
=====================
http://jessta.id.au

Nick

unread,
Sep 20, 2012, 10:28:22 AM9/20/12
to golan...@googlegroups.com, Nick, jes...@jessta.id.au
Thanks for the example. Actually I've never thought of using plain interface{} for deserializing JSON. I tried to use structs, but this seems to be much simpler, so I'm going to use just this.
Reply all
Reply to author
Forward
0 new messages