JSON(JavaScript Object Notation)
자바스크립트 객체 문법에 토대를 둔, 문자 기반의 데이터 교환 형식
Javascript 객체 리터럴 문법을 따르는 문자열
주로 중괄호 {}를 사용하여 객체를 나타내며, 객체 안에는 "키:값" 형식의 데이터가 들어간다
JSON 구조
{
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true,
"members": [
{
"name": "Molecule Man",
"age": 29,
"secretIdentity": "Dan Jukes",
"powers": ["Radiation resistance", "Turning tiny", "Radiation blast"]
},
{
"name": "Madame Uppercut",
"age": 39,
"secretIdentity": "Jane Wilson",
"powers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
{
"name": "Eternal Flame",
"age": 1000000,
"secretIdentity": "Unknown",
"powers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
}
]
}
stringify()
자바스크립트 객체 → JSON 문자열 변환
console.log(JSON.stringify({ x: 5, y: 6 }));
// Expected output: "{"x":5,"y":6}"
console.log(JSON.stringify([new Number(3), new String('false'), new Boolean(false)]));
// Expected output: "[3,"false",false]"
console.log(JSON.stringify({ x: [10, undefined, function(){}, Symbol('')] }));
// Expected output: "{"x":[10,null,null,null]}"
console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)));
// Expected output: ""2006-01-02T15:04:05.000Z""
parse()
JSON 문자열 → 자바스크립트 객체 변환
const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);
console.log(obj.count);
// Expected output: 42
console.log(obj.result);
// Expected output: true
'✍️ 스파르타 TIL' 카테고리의 다른 글
[TIL] sparta 34일차 - Live Share (0) | 2023.11.21 |
---|---|
[TIL] sparta 30일차 (0) | 2023.11.15 |
[TIL] sparta 28일차 - Redux Payload, Ducks 패턴 (0) | 2023.11.13 |
[TIL] sparta 27일차 - Redux (0) | 2023.11.10 |
[TIL] sparta 26일차 - memo, useContext, useCallback (0) | 2023.11.09 |