I’m stringyfing an object like {'foo': 'bar'}
How can I turn the string back to an object?
Advertisement
Answer
You need to JSON.parse()
your valid JSON string.
JavaScript
x
7
1
var str = '{"hello":"world"}';
2
try {
3
var obj = JSON.parse(str); // this is how you parse a string into JSON
4
document.body.innerHTML += obj.hello;
5
} catch (ex) {
6
console.error(ex);
7
}