Emmanuel Villegas Emmanuel Villegas -4 years ago 186
Javascript Question

how to parse string into json object javascript

I'm trying to parse this string into a JSON:


I'm doing it like so:

var strJSON = "{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}";

But I get the error message:

VM652:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1
at JSON.parse ()

Does anybody know what am I missing and how can i solve it?

Answer Source

You can replace single quotes to double quotes and parse it.

var str =  "{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}";

var o = JSON.parse(str.replace(/\'/g, "\""));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download