EN
JavaScript - JSON.stringify() reverse
1
answers
0
points
When I'm stringyfing an object like:
var object = { id: 2, name: 'Tom', age: 25 };
I get:
var jsonString = '{"id": 2, "name": "Tom", "age": 25}';
But how can I turn the jsonString
back to an object?
1 answer
0
points
The method you are looking for is JSON.parse()
.
Practical example
// ONLINE-RUNNER:browser;
var jsonString = '{"id": 2, "name": "Tom", "age": 25}';
try {
var object = JSON.parse(jsonString);
} catch (e) {
console.error(e);
}
console.log(object.id); // 2
console.log(object.name); // Tom
console.log(object.age); // 25
Note:
Put
JSON.parse()
into try-catch block, as the method can crash your code.
See also
References
0 comments
Add comment