/
json.html
154 lines (135 loc) · 3.24 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<script>
let user = {
name: "John",
age: 30,
toString() {
return `{name: "${this.name}", age: ${this.age}}`;
}
};
alert(user);
alert(typeof user);
let student={
name:"koushik",
age:28,
courses:["es6","c#","python","golang"],
spouse:null
};
let json=JSON.stringify(student);//converting object into json
alert(typeof json);
alert(JSON.stringify(1));
alert(JSON.stringify('test'));
alert(JSON.stringify(true));
alert(JSON.stringify([1,2,3,4]));
let user={
sayHi(){//ignored
alert("hello");
},
[Symbol("id")]:1234,//ignored
something:undefined//ignored
};
alert(JSON.stringify(user));
let meetup={
title:"Conference",
room:{
number:1234,
participants:["arnok","tony"],
}
};
alert(JSON.stringify(meetup))
alert(typeof meetup)
let room={
number:24
};
let meetup={
title:"conference"
participants:["arnok","stark"]
};
meetup.place=room;
room.occupiedBy=meetup;
JSON.strify(meetup);// Error: Converting circular structure to JSON*/
let room={
number:1234
};
let meetup={
title:"conference",
participating:[{name:"tony"},{name:"arnok"}],
place:room
};
room.occupiedBy=meetup;//room reference meetup
alert(JSON.stringify(meetup,['title','participating','place','name','number'],4))
alert(typeof meetup)
let user={
name:"arnok",
age:28,
roles:{
isAdmin:false,
isEditor:true
}
};
alert(JSON.stringify(user,null,4))
let room = {
number: 23
};
let meetup = {
title: "Conference",
participants: [{name: "John"}, {name: "Alice"}],
place: room // meetup references room
};
room.occupiedBy = meetup; // room references meetup
alert( JSON.stringify(meetup, function replacer(key, value) {
alert(`${key}: ${value}`); // to see what replacer gets
return (key == 'occupiedBy') ? undefined : value;
}));
number:24,
toJSON(){
return this.number;
};
let meetup={
title:"conference",
room
};
alert(JSON.stringify(room));
alert(JSON.stringify(meetup));
let numbers="[0,1,2,3,4]";
alert(typeof numbers);
numbers=JSON.parse(numbers);
alert(typeof numbers);
alert(numbers[4]);
let user='{"name":"arnok","age":28,"isAdmin":false,"freinds":[0,1,2,3]}';
alert(typeof user);
user=JSON.parse(user);
alert(typeof user);
alert(user);
alert(JSON.stringify(user,null,4));
alert(a);
json5 data
let customer={
name:'tony',
qualification:'b',//string value
item:7,
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON"
}
alert(JSON5.stringify(customer));
let str='{"title":"conference","date":"2017-11-30T12:00:00.000Z"}';
let date= new Date();
let meetup=JSON.parse(str,function(key,value){
if(key==='date') return new Date(value);
return value;
});
alert(meetup.date.getMonth());
let schedule = `{
"meetups": [
{"title":"Conference","date":"2017-11-30T12:00:00.000Z"},
{"title":"Birthday","date":"2017-04-18T12:00:00.000Z"}
]
}`;
schedule = JSON.parse(schedule, function(key, value) {
if (key == 'date') return new Date(value);
return value;
});
alert( schedule.meetups[0].date.getDate() ); // works!
</script>