Ad

Formatted String To Array

- 1 answer

In my program, I'm recieving a string such as this:

var string = '[[["540", "540"], ["570", "600"]], [["570", "570"]], [["600", "600"]], [["630", "630"]], [["660", "660"]]]';

Now obviously this string is in perfect array format. But its a string type. Of course I could make a big function that handles the parsing and builds the array, but it'll be slow and efficient. The string is already in array format, is there an existing function that'll let me convert that to an array type? It'll always be a 3d array if it matters.

Thanks

Ad

Answer

JSON.parse() can handle this kind of string perfectly.

var s = '[[["540", "540"], ["570", "600"]], [["570", "570"]], [["600", "600"]], [["630", "630"]], [["660", "660"]]]';

var j = JSON.parse(s);

document.write(j[0][0][0]);

Ad
source: stackoverflow.com
Ad