简体   繁体   English

2D数组JSON反序列化为字符串而不是数组

[英]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? 如何使用JavaScript将下面的JSON字符串反序列化为二维数组对象? If I use JSON.parse or eval it gets converted to a string. 如果我使用JSON.parseeval它将转换为字符串。 I'm using Douglas Crockford's JSON library. 我正在使用Douglas Crockford的JSON库。

[["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. 根据适用于我的@nemisj,更重要的是它应该工作,因为它是正确的表示形式。

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 ). 无论如何,使用Firebug(或使用eval任何Javascript控制台)逐步执行此操作应该相对简单。 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; 您当然可以正确使用Javscript,所以这里没有概念上的问题。 this is just one of those "d'oh!" 这只是那些“ 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. 例如,alert([1,2,3])返回一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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