简体   繁体   中英

turn string into an array?

If I have this below as a string, how can I easily make that into an array?

"[[0.01,4.99,0.01],[5,14.95,0.05]]"

I want the same result as:

var x = [[0.01,4.99,0.01],[5,14.95,0.05]];
var x = JSON.parse("[[0.01,4.99,0.01],[5,14.95,0.05]]");

Or the way jQuery does JSON parsing (better than eval ):

var x = (new Function("return " + "[[0.01,4.99,0.01],[5,14.95,0.05]]"))();

To make this answer complete, you can use a polyfill for older browsers to support JSON.parse and JSON.stringify . I recommend json3 , because Crockfords json2 is to crockfordy (insiders know what I mean).

var x = JSON.parse("[[0.01,4.99,0.01],[5,14.95,0.05]]");

对于没有内置JSON对象的旧浏览器,您可能希望下载Crockford的json2.js ,它定义了一个具有相同API的JSON对象(如果尚未定义)。

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