[thelist] string to multidimensional array
Ken Snyder
ksnyder at coremr.com
Mon Apr 2 10:02:26 CDT 2007
Daniel Kessler wrote:
> I have a string, '[[1,"myName"],[2,"myOtherName"]]'. I feed this to
> js as a variable. How do I turn this into a multidimensional array.
> So far, I can only create an array of length==1.
> Do I have to parse through it? So far I've been avoiding that.
>
> thanks.
>
You can simply do:
var multiDimensionalArray = eval('[[1,"myName"],[2,"myOtherName"]]');
However, consider using the more secure JSON methods (see below);
--Ken Snyder
/*
Parse a JSON text, producing a JavaScript value.
It returns false if there is a syntax error.
*/
var JSON = {};
JSON.decode: function (text) {
try {
return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
eval('(' + text + ')');
} catch (e) {
return false;
}
}
};
More information about the thelist
mailing list