-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJson.html
87 lines (46 loc) · 1.69 KB
/
Json.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html lang="en">
<head>
<title>JSON</title>
</head>
<body>
<div id="addbutton">add to storage</div>
<div id="seebutton">view storage</div>
<input type="text" id="name">
<input type="text" id="last">
<script>
document.getElementById("addbutton").addEventListener("click", addtostorage);
document.getElementById("seebutton").addEventListener("click", viewstorage);
//addparse here to hold the storage content to the local host
const people=JSON.parse(localStorage.getItem("tester1")) || {"name":"none","last":"none"}
console.log(people);
function addtostorage(){
let tempname=document.getElementById("name").value;
let templast=document.getElementById("last").value
//convert it into object and stringify
let myobj=JSON.stringify({"name":tempname,"last":templast});
console.log(myobj);
localStorage.setItem("tester1", myJSON);
console.log("clicked" + myobj);
}
function viewstorage(){
let tempholder=JSON.parse(localStorage.getItem("tester1")) || people;
//parse the data here to manipulate
console.log(tempholder );
}
var myJSON={"name":"valola",
"last":"galoa",
"age":76};
//STEP1. getting data and convert to string
var myJSONstringconvert=JSON.stringify(myJSON);
//STEP2. covert to object(useable format) and working with it
var myJSONparsed=JSON.parse(myJSONstringconvert);
//STEP3. update any values
myJSONparsed.name="kalolo";
console.log(myJSONparsed);
//STEP4. convert data back to string and send data
var senddata=JSON.stringify(myJSONparsed);
console.log(senddata);
</script>
</body>
</html>