Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have receive a string with this format

"{'key': value}, {'key': value},{'key': value} ..."

I need to convert this string to javascript array to use like this:

var $arrayValue = [{'key': value}, {'key': value},{'key': value}];

how to conver this string into array?

share|improve this question
add comment

1 Answer

string = string = '{"key": "value"}, {"key": "value"}'
JSON.parse("[" + string + "]")
share|improve this answer
1  
Assuming you can be sure each object is valid JSON, this is the best way to do it. –  Dan Sep 30 '13 at 5:03
 
The console get me this error: Uncaught SyntaxError: Unexpected token ' –  xzegga Sep 30 '13 at 5:07
1  
This is because you're passing in single quoted JSON keys/values. Use double quotes. –  Ryan Bigg Sep 30 '13 at 5:22
 
This string is a variable extracted from json query, this is automatically extracted with string double quotes "{'key': value}", if i replace the single quote with double this string fail: string = string = "{"key": "value"}, {"key": "value"}" notice the double quotes before and after string :S –  xzegga Sep 30 '13 at 5:41
 
Instead your example code, I have this: string = string = myVariable; JSON.parse("[" + string + "]") This variable is automatically placed with double quotes "", –  xzegga Sep 30 '13 at 6:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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