Parsing JSON

Although JSON is based on JavaScript, a JSON file is not a JavaScript object. It's a string. To convert it into an object and access its properties with the dot operator, you can use JSON.parse():

const obj = JSON.parse(jsonString);

Sometimes you need to convert a JavaScript object back into JSON format. You might also do this for debugging. You can do this with JSON.stringify():

const jsonString = JSON.stringify(obj);

If you parsed the example JSON file at the beginning of this section, the JavaScript object will actually be an array of objects, and you can list its contents (in the JavaScript console) using the following code (Examples/example-14.html):

obj.forEach(function(item) {
console.log(item.continent, +item.population, +item.areakm2);
});
..................Content has been hidden....................

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