-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.js
46 lines (38 loc) · 963 Bytes
/
json.js
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
// 1.Object to JSON
// Stromgfy(obj)
let json=JSON.stringify(true);
console.log(json);
json=JSON.stringify(['apple','banana']);
console.log(json);
const rabbit={
name:'tori',
color:'white',
size:'null',
birthDate:new Date(),
jump:()=>{
console.log(`${name} can jum[!]`);
},
};
json=JSON.stringify(rabbit);
console.log(json);
json=JSON.stringify(rabbit,['name','color','size']);
console.log(json);
json=JSON.stringify(rabbit,(key,value)=>{
console.log(`key:${key}, value:${value}`);
return key==='name'?'ellie':value;
});
console.log(json);
// 2.JSON to Object
// parse(json)
console.clear();
json=JSON.stringify(rabbit);
console.log(json);
const obj=JSON.parse(json,(key,value)=>{
console.log(`key: ${key}, value: ${value}`);
return key==='birthDate' ? new Date(value) : value;
});
console.log(obj);
rabbit.jump();
//obj.jump();
console.log(rabbit.birthDate.getDate());
console.log(obj.birthDate.getDate());