views:

263

answers:

2

How can I deserialize the string of JSON below into a two-dimensional array object using JavaScript? If I use JSON.parse or eval it gets converted to a string. I'm using Douglas Crockford's JSON library.

[["Apples", "21529", "22457"], ["Apricots", "12547", "12559"]]
A: 

Is that the actual string you're deserialising? As per @nemisj that works for me, and more importantly it should work as it's the correct representation.

I suspect you may have a dangling unclosed bracket somewhere, or some other simple yet hard-to-notice problem like that, which is causing your deserialisation to fail.

In any case, this should be relatively simple to step through with Firebug (or any Javascript console using eval). Just keep reducing your test input until you either end up with a minimal case that still fails and you can see what the problem is; or it suddenly starts working when you strip out a particular layer of chaff, and then you know the problem lies there.

You're certainly using Javscript correctly so there's no conceptual problem here; this is just one of those "d'oh!" issues that you need to pin down yourself.

Andrzej Doyle
A: 

How are you reading the returned value? An array will convert itself to a string at the drop of a hat-, if it thinks you want a string. alert([1,2,3]) returns a string, for example.

kennebec