up vote 0 down vote favorite

I'm retrieving an array of objects from a hidden html input field. The string I'm getting is:

"{"id":"1234","name":"john smith","email":"[email protected]"},{"id":"4431","name":"marry doe","email":"[email protected]"}"

Now I need to pass this as an array of objects again. How do I convert this string into array of objects?

flag

Is it wrapped in []? – SLaks Apr 26 at 0:42

2 Answers

up vote 1 down vote accepted
var array_of_objects = eval("[" + my_string + "]");

This executes the string as code, which is why we need to add the [] to make it an object. This is also one of the few legitimate uses for eval as its the fastest and easiest way. :D

link|flag
FYI, you can also use JSON.parse method if you are using JSON parser library. Thanks SLaks stackoverflow.com/questions/2710556/… – dev.e.loper Apr 26 at 0:58
up vote 2 down vote

Assuming that str holds valid JSON syntax, you can simply call eval(str).

For security reasons, it's better to use a JSON parser, like this:

JSON.parse(str);

Note that str must be wrapped in [] to be a valid JSON array.

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.