Skip to content Skip to sidebar Skip to footer

How To Convert This Javascript String Into Javascript Array/object

Possible Duplicate: How to parse JSON easily? I have this string: [{text: 'First Option', value: 'first'},{text: 'Second Option', value: 'second'},{text: 'Third Option', value

Solution 1:

Could either use var data = JSON.parse(yourString); or var data = eval('(' + yourString+ ')');

Solution 2:

This is one of the times that eval actually comes in useful:

var x = eval(yourString);

But it's definitely safer to use JSON.parse as suggested by other answers.

Here's a working example of the eval version.

Solution 3:

Solution 4:

Just set a variable to exactly that string.

var new_object = [{text: 'FirstOption',  value: 'first'},{text: 'SecondOption', value: 'second'},{text: 'ThirdOption',  value: 'third'}]

Solution 5:

If you jave jQuery, you can use jQuery.parseJSON. If you don't you can pull the function from the source code and place it on your page and have the same advantages.

Post a Comment for "How To Convert This Javascript String Into Javascript Array/object"