Convert string array representation back to an array

Go To


I have a string representation of an array:

'["item1", "item2", "item3"]'

I'm trying to convert this back to an array.

Any pointers appreciated.

2012-04-04 01:16
by Chin
That looks an awful lot like a JSON string. Just use the "standard" JSON script - inspector-g 2012-04-04 01:20


Just use JSON.parse('["item1", "item2", "item3"]');

2012-04-04 01:19
by stewe
worked well thanks - Chin 2012-04-04 01:24


Try JSON.parse:

 ary = JSON.parse(s);

Most browsers support it natively, for others you'll need json.js

2012-04-04 01:19
by georg
Note that if the string is not valid JSON (for instance if it uses single quotes), this will throw an exception. You could also use eval for this task and it will accept single quotes, but make sure you think it through first - Jesse 2012-04-04 01:22
thanks for the advice - Chin 2012-04-04 01:41


You also can Use

var genericList = eval('(' + s + ')');

   alert("element : " + genericList[i]);
2012-04-04 01:24
by Yogesh Prajapati