1var person={"first_name":"Tony","last_name":"Hawk","age":31};
2var personJSONString=JSON.stringify(person);
1// Storing data:
2myObj = {name: "John", age: 31, city: "New York"};
3myJSON = JSON.stringify(myObj);
4localStorage.setItem("testJSON", myJSON);
5
6// Retrieving data:
7text = localStorage.getItem("testJSON");
8obj = JSON.parse(text);
9document.getElementById("demo").innerHTML = obj.name;
1var jsonPerson = '{"first_name":"billy", "age":23}';
2var personObject = JSON.parse(jsonPerson); //parse json string into JS object
1var str = '{"names":[' + // crate JSON object
2'{"first":"Hakuna","lastN":"Matata" },' +
3'{"first":"Jane","lastN":"Doe" },' +
4'{"first":"Air","last":"Jordan" }]}';
5obj = JSON.parse(str); // parse
6document.write(obj.names[1].first); // access
7
1var obj = {name: 'John', age: 20}
2
3// to json
4var json = JSON.stringify(obj);
5
6//json to object
7var obj1 = JSON.parse(json);