JSON.stringify() 메서드는 JavaScript의 값이나 객체를 JSON 문자열로 변환한다.
JSON.stringify()(value,replacer,space)
replacer 예시(함수)function replacer(key, value) {
if (typeof value === ‘string’) {
return undefined;
}
return value;
}
var foo = {name: ‘jason’, nickname: ‘ball’, weight: 75};
var useJson = JSON.stringify(foo, replacer);
console.log(useJson) // {"weight":75}
replacer 예시(배열)
var foo = {name: ‘jason’, nickname: ‘ball’, weight: 75};
var useJson = JSON.stringify(foo, [‘nickname’, ‘weight’]);
console.log(useJson); // {“nickname”:”ball”,”weight”:75}
space(string)
JSON.stringify({a:2},null,'string');
/* 결과
"{
string"a": 2
}"*/
space(number)
JSON.stringify({a:2},null,5);
/* 결과
"{
"a": 2
}"
*/