WEB
JSON이 뭔지 알아보자
Kwon_
2021. 3. 22. 14:22
JSON ?
JSON(JavaScript Object Notation)은 자바스크립트 문법을 빌린 데이터 교환 형식이다.
{
"title": "기록좀하자 인마",
"description": "기록좀하자 인마의 Blog",
"owner": "Kwon Hyeok Jin",
"createdAt": "2020-03-00",
"keyword": ["javascript", "Java", "MySQL", "WEB"],
}
JSON 함수
-JSON.parse()
JSON형태의 String을 객체형 object로 변경하는 메소드이다.
데이터통신시 쿼리스트링 등 교환을 위해서는 문자형으로 변환이 필요하고, 이 주고받은 걸 다루기 위해서 다시 객체형으로 바꾸어 쓸 때 사용한다.
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
-JSON.stringify()
반대로 JSON Object형을 String타입으로 변경하는 메소드이다.
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""