Converting JSON to JavaScript Objects

A JSON string represents a JavaScript object in string form. The string syntax is very similar to code, so it’s easy to understand. You can use the JSON.parse(string) method to convert a string that is properly formatted with JSON into a JavaScript object.

For example, in the following code snippet, notice that accountStr is defined as a formatted JSON string, then converted to a JavaScript object using JSON.parse() and then member properties can be accessed via dot notation:

var accountStr = '{"name":"Jedi", "members":["Yoda","Obi Wan"],
                   "number":34512, "location": "A galaxy far, far away"}';
var accountObj = JSON.parse(accountStr);
console.log(accountObj.name);
console.log(accountObj.members);

This is the output from the code above:

Jedi
[ 'Yoda', 'Obi Wan' ]

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.237.58