- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.js
More file actions
Latest commit
52 lines (41 loc) · 1.42 KB
/
Copy pathjson.js
File metadata and controls
52 lines (41 loc) · 1.42 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
'use strict';
// JSON
// JavaScript Object Notation
// 1. Object to JSON
// stringfy(obj)
letjson=JSON.stringify(true);
console.log(json);
json=JSON.stringify(['apple','banana']);
console.log(json);// ["apple","banana"]
constrabbit={
name: 'tori',
color: 'white',
size: null,
brithDate: newDate(),
jump: ()=>{
console.log(`${name} can jump!`);
},
};
json=JSON.stringify(rabbit);
console.log(json);// jump라는 함수는 JSON에 포함되지 않음! 함수는 object에 있는 데이터가 아니기 때문! symbol같은 자바스크립트에서만 있는 특별한 데이터도 포함되지 않음
json=JSON.stringify(rabbit,['name']);// 원하는 property만 골라서 정의하면 해당 property만 JSON으로 변환
console.log(json);
json=JSON.stringify(rabbit,(key,value)=>{
console.log(`key: ${key}, value: ${value}`);
// return value;
returnkey==='name' ? 'sill' : value;
});
console.log(json);
// 2. JSON to Object
// parse(json)
console.clear();
json=JSON.stringify(rabbit);
constobj=JSON.parse(json,(key,value)=>{
console.log(`key: ${key}, value: ${value}`);
returnkey==='brithDate' ? newDate(value) : value;
});// string으로 serialize된 obj(함수 포함되지 않음!)으로부터 JSON을 만듦
console.log(obj);
rabbit.jump();
// obj.jump(); // Error!
console.log(rabbit.brithDate.getDate());
console.log(obj.brithDate.getDate());