I'm working on a web application witch javascript, and I have to receive and parse a JSON string that looks like this:
{name:"", house:""}
What's the best way to convert it to proper notation?
{"name":"", "house":""}
Thanks in advance!
I'm working on a web application witch javascript, and I have to receive and parse a JSON string that looks like this:
{name:"", house:""}
What's the best way to convert it to proper notation?
{"name":"", "house":""}
Thanks in advance!
var str = '{name:"", house:""}';
var newStr = JSON.stringify( eval( '(' + str + ')' ) );
console.log(newStr); //{"name":"", "house":""}
Do not use eval if the data source is untrusted though.
By the way, are you sure you're receiving a badly formed JSON string and it is not just an object? In case you're using jQuery, it automatically parses JSON responses into objects. In that case you'd just call JSON.stringify passing the object to make a valid JSON string from it, or access the request's responseText. Fiddle with jQuery Ajax.