开发者

2D array JSON deserializes into a string not an array

How can I deseri开发者_运维技巧alize 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"]]


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜