简体   繁体   中英

2D array JSON deserializes into a string not an array

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"]]

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM