22

I have a string representation of an array:

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

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

Any pointers appreciated.

1
  • That looks an awful lot like a JSON string. Just use the "standard" JSON script? Commented Apr 4, 2012 at 1:20

3 Answers 3

34

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

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

Comments

3

Try JSON.parse:

 ary = JSON.parse(s);

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

1 Comment

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.
-1

You also can Use

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

for(i=0;i<genericList.lengthl;i++){
   alert("element : " + genericList[i]);
}

Comments

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.