다른 언어도 사용하지만, 완전하게 같지 않을수도 있습니다.
⇒ 어떤 언어의 특정 버전은 지원을 안할수도 있기 때문에 변환 기준에 따라 확인이 필요합니다.
<aside> 💡 JSON.stringify(value[, replacer[, space]])
</aside>
매개변수
value : JSON문자열로 변환할 값
console.log(JSON.stringify({book:'책', price: 1000}));//{"book":"책","price":1000}
console.log(JSON.stringify([123,"책", "Java"]));//[123,"책","Java"]
console.log(JSON.stringify([undefined, NaN, null, Infinity]));//[null,null,null,null]
console.log(JSON.stringify([true, false]));[true, false]));//[true, false]
console.log(JSON.stringify(undefined));//undefined
console.log(JSON.stringify([undefined]));//[null]
console.log(JSON.stringify({value:undefined}));//{}
replacer(optional): 문자열화 동작 방식을 변경하는 함수 함수일 때 문자열화 될 key와 value 매개변수를 받습니다. 그리고 해당 객체가 replacer의 this
매개변수로 제공됩니다.
var obj = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};
function replacer(key, value){
if(key === "model")return "openedBox"
return value;
}
var jsonString = JSON.stringify(obj, replacer);
//"{"foundation":"Mozilla","model":"openedBox","week":45,"transport":"car","month":7}"
space(optional) : 가독성을 목적으로 JSON문자열 출력에 공백을 삽입하는데 사용되는 String or Number객체
var obj = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};
function replacer(key, value){
if(key === "model")return "openedBox"
return value;
}
var jsonString = JSON.stringify(obj, replacer, "\\n");
/*
{
"foundation": "Mozilla",
"model": "openedBox",
"week": 45,
"transport": "car",
"month": 7
}
*/
<aside> 💡 JSON.parse(text[, reviver])
</aside>