JSON을 문자열로 변환기켜주는 Method입니다.

문자로 변환을 할 경우에, Serialize를 합니다.

예를 들면 new Date()의 경우엔, toJSON()이 실행되고, Serialize된 값을

문자로 변환한다.

JSON.stringify ( value [ , replacer [ , space ] ] )

value: 문자열을 만드는 JSON
replacer: 함수나 배열로 출력하는 요소를 지정
space: indent를 문자로 지정
function replacer(key, value) {
  // Filtering out properties
  if (typeof value === 'string') {
    return undefined;
  }
  return value;
}

var foo = {foundation: 'Mozilla', model: 'box', week: 45, transport: 'car', month: 7};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'
JSON.stringify(foo, ['week', 'month']);
// '{"week":45,"month":7}'



JSON.stringify({ a: 2 }, null, ' ');
// '{
//  "a": 2
// }'

JSON.stringify({ uno: 1, dos: 2 }, null, '\t');
// returns the string:
// '{
//     "uno": 1,
//     "dos": 2
// }'

 

'Javascript' 카테고리의 다른 글

shallow copy & deep copy  (0) 2022.06.19
Object.assign 의 사용법  (0) 2022.06.19
배열 조작  (0) 2022.06.19

+ Recent posts