Fastest way to split a string into a multidimensional array of arbitrary dimensions in JavaScript?

I'm trying to determine the fastest way to split a string in JavaScript into a multidimensional array with arbitary dimensions. Is there a way better than looping over the string and matching brackets of the same depth?

Example string:

[1,2,3,[1,2,3,[1,2,3]],4,5,[1,2],[[1,2],[1,2]],[1],[1]]

Answers


If you don't need to count for older browsers, you can use JSON.parse:

var a = '[1,2,3,[1,2,3,[1,2,3]],4,5,[1,2],[[1,2],[1,2]],[1],[1]]';

var b = JSON.parse(a);

For older browsers I can think of one quick and dirty way, some use it as last resort of parsing JSON.

var a = '[1,2,3,[1,2,3,[1,2,3]],4,5,[1,2],[[1,2],[1,2]],[1],[1]]';

var b = (new Function('return ' + a + ';'))();

Need Your Help

Parsing complex JSON

json parsing windows-phone-7 windows-phone-7.1

I want to parse a complex JSON on WP7. First I have to parse a JSON feed and I retrieve data to parse second JSON feed.

Cannot run node-redis-session lib

node.js session cookies redis

I've installed the node-redis-session dependency and in the first time runs fine, but i've deleted the created cookies of the browser, and now I cannot make it run again.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.