简体   繁体   中英

How to convert string to javascript array

I had a string as

    string flotdata="[[1,2],[3,6],[8,10]]"

How to convert this to javascript array so that i can use it in jquery flot pluggin series data.

This string was returned to script there it needs to be converted into javascript array

Use JSON.parse:

var a = JSON.parse("[[1,2],[3,6],[8,10]]");

Edit

JSON.parse documentation on MDN

You can use the built-in JSON.parse() function:

flotdata = "[[1,2],[3,6],[8,10]]";
var my_obj = JSON.parse( flotdata );

// [ [Array[2],Array[2],Array[2]] ]

Reference - JSON.parse()

Parse a string as JSON, optionally transforming the value produced by parsing.

An alternative is:

flotdata = "[[1,2],[3,6],[8,10]]";
flotdata = eval(flotdata);

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