简体   繁体   English

将JSON(作为字符串)转换为多维数组

[英]Turn JSON (as String) into Multidimensional Array

I'm trying to take a hideous JSON string - it shows up in the console like this: 我正在尝试使用一个丑陋的JSON字符串-它在控制台中显示如下:

"[[\"a\",1],[\"b\",2],[\"c\",3],[\"d\",4]]"

... and turn it into a multidimensional array for use in Flot.js. ...并将其转换为多维数组以在Flot.js中使用。

Flot.js will take in input like this just fine: Flot.js将像这样很好地接受输入:

var data = [["January", 10], ["February", 8], ["March", 4], ["April", 13], ["May", 17], ["June", 9]];

But not my code ... 但不是我的代码...

var data = "[[\"a\",1],[\"b\",2],[\"c\",3],[\"d\",4]]";
var parsedData = JSON.parse(data);

... nor this ... ...也不是...

var data = [\"a\",1],[\"b\",2],[\"c\",3],[\"d\",4];
var parsedData = JSON.parse(data);

... etc. What approach should I be taking? ...等。我应该采用哪种方法?

Looks like I found the solution for this particular case. 看来我找到了这种特殊情况的解决方案。 Starting with the data object: 从数据对象开始:

var s = jQuery.parseJSON(data);
var t = "[" + s + "]";
var u = jQuery.parseJSON(t);

the u object becomes acceptable input for Flot.js. u对象成为Flot.js可接受的输入。 Although I'm not entirely sure if this is the best approach, or why this worked. 尽管我不完全确定这是否是最好的方法,还是为什么行得通。

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

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