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?

1 Answer 1

3
string = string = '{"key": "value"}, {"key": "value"}'
JSON.parse("[" + string + "]")
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

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.