1

I have the following string:

var arr = "[{'one' : '1'}, {'two' : '2'}]";

How can I pass this string to an object?

I've tried the following but it doesn't seem to work:

arr.split(',');
2
  • What do you mean by passing the string to object ? Commented Sep 27, 2015 at 22:08
  • 2
    If this string is coming from a data source, (e.g you're not typing it, and you have no control over it) you should tell them that it is invalid JSON. Commented Sep 27, 2015 at 22:12

1 Answer 1

4

you must first transform single quotes to double quotes then use JSON.parse

JSON.parse(arr.replace(/'/g,'"'))

Sign up to request clarification or add additional context in comments.

Comments

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.