0

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?

Dan Hlavenka
3,8308 gold badges44 silver badges63 bronze badges
asked Sep 30, 2013 at 4:58

1 Answer 1

3
string = string = '{"key": "value"}, {"key": "value"}'
JSON.parse("[" + string + "]")
answered Sep 30, 2013 at 5:01
Sign up to request clarification or add additional context in comments.

5 Comments

Assuming you can be sure each object is valid JSON, this is the best way to do it.
The console get me this error: Uncaught SyntaxError: Unexpected token '
This is because you're passing in single quoted JSON keys/values. Use double quotes.
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
Instead your example code, I have this: string = string = myVariable; JSON.parse("[" + string + "]") This variable is automatically placed with double quotes "",

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.