0

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

Nat
  • 890
  • 3
  • 11
  • 23

1 Answers1

3

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]);
lleaff
  • 4,249
  • 17
  • 23
  • I knew there was a simplistic solution. The string doesn't look anything like JSON format that I've seen before, but I guess it doesn't matter? Thanks! – Nat Jan 02 '16 at 02:09
  • It is valid JSON, arrays are one of allowed types, even at the top level, see: http://stackoverflow.com/questions/3833299/can-an-array-be-top-level-json-text – lleaff Jan 02 '16 at 02:11
  • @Nathan This *is* JSON... Especially when JSON stands for JavaScript Object Notation (hint hint) – Derek 朕會功夫 Jan 02 '16 at 02:14