Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert this javascript string into javascript array/object [duplicate]

Tags:

javascript

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: 'third'}]

How do I convert it into an array/object in the same form in javascript?

like image 232
user991987 Avatar asked Oct 12 '11 18:10

user991987


2 Answers

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

like image 112
Ritesh Mengji Avatar answered Sep 19 '22 17:09

Ritesh Mengji


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.

like image 20
James Allardice Avatar answered Sep 21 '22 17:09

James Allardice