๐ฅ JSON ํน์ง
JSON.stringify
โ ๊ฐ์ฒด๋ฅผ JSON์ผ๋ก ๋ฐ๊ฟ์ค๋๋ค.
JSON.parse
โ JSON์ ๊ฐ์ฒด๋ก ๋ฐ๊ฟ์ค๋๋ค.
// 1) Object to JSON >> stringify API // Boolean ํํ JSON์ผ๋ก ๋ณํ let json = JSON.stringify(true); console.log(json) // true // Array ํํ JSON์ผ๋ก ๋ณํ let json = JSON.stringify( ['a', 'b', 'c'] ); console.log(json) // ["a", "b", "c"] // String ํํ JSON์ผ๋ก ๋ณํ let json = JSON.stringify('text'); console.log(json) // "text" // Object ํํ JSON์ผ๋ก ๋ณํ const rabbit = { name: 'nana', color: 'white', size: null, birthDate: new Date(), jump: () => { console.log(`${name} can jump!`); }, }; let json = JSON.stringify(rabbit); console.log(json) // {"name": "nana", "color": "white", "size": null, "birthDate": "2021-04-07T20:22.670Z" } // jump function ํํ๋ JSON์ผ๋ก ๋ณํ๋์ง ์๋๋ค. // ์ค์ฒฉ ๊ฐ์ฒด๋ JSON์ผ๋ก ๋ณํ const meetup = { title: "Conference", room: { number: 23, participants: ["john", "ann"] } }; let json = JSON.stringify(meetup); console.log(json) //{ // "title":"Conference", // "room":{"number":23,"participants":["john","ann"]}, // } // 2) JSON to Object >> parse API let json = JSON.stringify(rabbit); // JSON์ผ๋ก ๋ณํ let obj = JSON.parse(json); // ๋ค์ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๋ณํ console.log(obj)
๐ฅ JSON ๋ฌธ๋ฒ
{ "employees": [ { "name": "Yerim", "lastName": "Ko" }, { "name": "Ajin", "lastName": "Woo" }, { "name": "Bokja", "lastName": "Park" } ] }