-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.js
More file actions
63 lines (43 loc) · 1.5 KB
/
Copy pathjson.js
File metadata and controls
63 lines (43 loc) · 1.5 KB
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
// 1. Object to JSON
// stringify(obj)
let json = JSON.stringify(true);
console.log(json);
console.log(typeof json);
json = JSON.stringify(['aaa','bbb','ccc']);
console.log(json); // ["aaa", "bbb", "ccc"]
console.log(typeof json); // string type
const rabbit = {
name : 'toto',
color : 'white',
size: null,
birthDate : new Date(),
jump: () => {
console.log(`${name} can jump`)
},
}
json = JSON.stringify(rabbit);
console.log(json);
// {"name":"toto","color":"white","size":null,"birthDate":"2023-05-02T15:14:55.126Z"}
console.log(typeof json); // string type
json = JSON.stringify(rabbit,['name','color']);
console.log(json); // { "name" : "toto"}
console.log(typeof json); // string type
json = JSON.stringify(rabbit,(key, value) => {
console.log(`key: ${key}, value: ${value}`);
return value;
});
console.log(typeof json); // string type
console.clear(); // 정리~~~~~
// 2.json parse
json = JSON.stringify(rabbit);
const obj = JSON.parse(json, (key, value) => {
console.log(`key: ${key}, value: ${value}`);
return key === "birthDate" ? new Date(value) : value;
// key가 birthDate일때, String타입이었던 value를 date()로 변경하고,
// birthDate가 아니면 기존의 value그대로 return하겠다.
});
console.log(obj);
rabbit.jump(); // can jump -> jump()실행됨.
//obj.jump(); // json.js:51 Uncaught TypeError: obj.jump is not a function at json.js:51:5
console.log(rabbit.birthDate.getDate());
console.log(obj.birthDate.getDate());