{
"name" : "Sam",
"age" : 20,
"moto" : ["hustlin","grindin"]
}
const person = {name:'kang', age:27};
JSON.stringify(person); // '{"name":"kang","age":27}'
JSON.stringify(person,null,2); // '{\n "name": "kang",\n "age": 27\n}'
// replacer 함수
// 값의 타입이 Number이면 필터링되어 반환되지 않는다.
const filter = (key,value) => {
// undefined: 반환하지 않음
return typeof value === 'number'
? undefined
: value;
}
JSON.stringify(person,filter); // '{"name":"kang"}'
const obj = {
name: 'Lee',
age: 20,
alive: true,
hobby: ['traveling', 'tennis']
}
const json = JSON.stringify(obj);
const parsed = JSON.parse(json);
console.log(typeof parsed, parsed);
// object {name: 'Lee', age: 20, alive: true, hobby: Array(2)}
const todos = [
{id: 1, content: 'HTML', completed: false},
{id: 2, content: 'CSS', completed: true},
{id: 3, content: 'Javascript', completed: false}
];
const json = JSON.stringify(todos);
const parsed = JSON.parse(json);
console.log(typeof parsed, parsed);
/*
object [
{id: 1, content: 'HTML', completed: false},
{id: 2, content: 'CSS', completed: true},
{id: 3, content: 'Javascript', completed: false}
]
*/
(docs) 모던 자바스크립트 43강 Ajax