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

1 Answer 1

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]);

Sign up to request clarification or add additional context in comments.

3 Comments

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!
It is valid JSON, arrays are one of allowed types, even at the top level, see: stackoverflow.com/questions/3833299/…
@Nathan This is JSON... Especially when JSON stands for JavaScript Object Notation (hint hint)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.