1[
2 {
3 "id": "0001",
4 "type": "donut",
5 "name": "Cake",
6 "ppu": 0.55,
7 "batters":
8 {
9 "batter":
10 [
11 { "id": "1001", "type": "Regular" },
12 { "id": "1002", "type": "Chocolate" },
13 { "id": "1003", "type": "Blueberry" },
14 { "id": "1004", "type": "Devil's Food" }
15 ]
16 },
17 "topping":
18 [
19 { "id": "5001", "type": "None" },
20 { "id": "5002", "type": "Glazed" },
21 { "id": "5005", "type": "Sugar" },
22 { "id": "5007", "type": "Powdered Sugar" },
23 { "id": "5006", "type": "Chocolate with Sprinkles" },
24 { "id": "5003", "type": "Chocolate" },
25 { "id": "5004", "type": "Maple" }
26 ]
27 },
28 {
29 "id": "0002",
30 "type": "donut",
31 "name": "Raised",
32 "ppu": 0.55,
33 "batters":
34 {
35 "batter":
36 [
37 { "id": "1001", "type": "Regular" }
38 ]
39 },
40 "topping":
41 [
42 { "id": "5001", "type": "None" },
43 { "id": "5002", "type": "Glazed" },
44 { "id": "5005", "type": "Sugar" },
45 { "id": "5003", "type": "Chocolate" },
46 { "id": "5004", "type": "Maple" }
47 ]
48 },
49 {
50 "id": "0003",
51 "type": "donut",
52 "name": "Old Fashioned",
53 "ppu": 0.55,
54 "batters":
55 {
56 "batter":
57 [
58 { "id": "1001", "type": "Regular" },
59 { "id": "1002", "type": "Chocolate" }
60 ]
61 },
62 "topping":
63 [
64 { "id": "5001", "type": "None" },
65 { "id": "5002", "type": "Glazed" },
66 { "id": "5003", "type": "Chocolate" },
67 { "id": "5004", "type": "Maple" }
68 ]
69 }
70]
71
1var obj = {name: "Martin", age: 30, country: "United States"};
2
3// Converting JS object to JSON string
4var json = JSON.stringify(obj);
5
6console.log(json);
7// Prints: {"name":"Martin","age":30,"country":"United States"}
8"https://www.tutorialrepublic.com/faq/how-to-convert-js-object-to-json-string.php"
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