Javascript Data Type How to - Create object from a JSON string








Question

We would like to know how to create object from a JSON string.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
var s = '{ "name": "John", "family": "Resig" }';
var o = JSON.parse(s);
document.writeln(o.name); // John
document.writeln('<br/>');
document.writeln(o.family); // Resig
<!--from   w  w  w  . j av  a2s .co  m-->
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: