-
9. JSONJavaScript & TypeScript 2020. 11. 15. 13:21
JSON (JavaScript Object Notation)
- simplest data interchange format
- lightweight text-based structure
- easy to read
- key - value pairs
- used for serialization and transmission of data between the network and the network connection
- independent programming language and platform
1. Object to JSON
- stringify (obj)
let json = JSON.stringify(true); console.log(json); // true json = JSON.stringify(["apple", "banana"]); console.log(json); // ["apple", "banana"] const rabbit = { name: 'tori', color: 'white', size: null, birthDate: new Date(), jump: () => { console.log(`${name} can jump!`); } }; json = JSON.stringify(rabbit); // method와 symbol은 X console.log(json); // {"name":"tori","color":"white","size":"null, // "birthDate":"2020-05-29T13:20:22.670Z"} json = JSON.stringify(rabbit, ['name']); // {"name":"tori"} json = JSON.stringify(rabbit, (key, value) => { return key === 'name' ? 'ellie' : value; });
2. JSON to Obejct
- parse (json)
json = JSON.stringify(rabbit); const obj = JSON.parse(json, (key, value) => { return key === 'birthDate' ? new Date(value) : value; }); console.log(obj); rabbit.jump(); // can jump ! obj.jump(); // error console.log(rabbit.birthDate.getDate); // 29 console.log(obj.birthDate.getDate()); // 29
※출처
'JavaScript & TypeScript' 카테고리의 다른 글
11. Promise (0) 2020.11.15 10. Callback (0) 2020.11.15 8. Array APIs (0) 2020.11.14 7. Array, API (0) 2020.11.14 6. what is object (0) 2020.11.14