-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.js
More file actions
55 lines (45 loc) · 1.44 KB
/
json.js
File metadata and controls
55 lines (45 loc) · 1.44 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
// JSON
// JavaScript Object Notation
// 1. Object to JSON
// stringify(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(),
// symbol: Symbol('id'), // javascript에만 있는 특별한 data는 json 형식에 포함되지 않음
// 함수는 object에 있는 데이터가 아니므로 json 형식에 포함되지 않음
jump: () => {
console.log(`${name} can jump!`);
},
}
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' ? 'dah' : value;
});
console.log(json);
// 2. JSON to Object
// parse(json)
console.clear();
json = JSON.stringify(rabbit);
console.log(json);
const obj = JSON.parse(json);
console.log(obj);
rabbit.jump();
// obj.jump(); // 함수는 serialize 될 때 포함이 안되므로 obj에 해당 기능이 없음
console.log(rabbit.birthDate.getDate());
// console.log(obj.birthDate.getDate()); // obj의 birthDate는 string이므로 실행 X
const obj2 = JSON.parse(json, (key, value) => {
console.log(`key: ${key}, value: ${value}`);
return key === 'birthDate' ? new Date(value) : value;
});
console.log(obj2);
console.log(obj2.birthDate.getDate());