최대 1 분 소요

JSON

image

image

  1. 데이터를 주고받을수 있는 가장 간단한 파일 포멧
  2. 텍스트 기반
  3. 가독성 좋음
  4. 키와 벨류로 이루어짐
  5. 데이터를 직렬화 및 전송할때 쓰임
  6. 프로그래밍 언어나 플랫폼 상관없이 사용할 수 있음

1. Object to JSON = stringfy(obj)

let json = JSON.stringify  (true);
console.log(json);

image

json=JSON.stringify(['apple','banana']);
console.log(json);

image

const rabbit={
  name: 'tori',
  color: 'white',
  size: null,
  birthDate: newDate(),
  jump: function(){
    console.log(`${this.name}can jump!`);
  },
};

json = JSON.stringify(rabbit);
console.log(json);

image

* 함수는 변환되지 않음

json = JSON.stringify(rabbit,[‘name’,’color’,’size’]); console.log(json);

image

* 원하는 목록만 변환가능

json = JSON.stringify(rabbit,(key,value)=>{ console.log(key: ${key}, value: ${value}); return key === ‘name’? ‘ellie’: value; }); console.log(json);

image

2. JSON to Object = parse(json)

```js json = JSON.stringify(rabbit); console.log(json);

const obj = JSON.parse(json,(key,value)=>{ console.log(key: ${key}, value: ${value}); returnkey===’birthDate’? newDate(value): value; });

console.log(obj); rabbit.jump(); // obj.jump(); console.log(rabbit.birthDate.getDate()); console.log(obj.birthDate.getDate());

image

*Overloading = 몇 개의 파라미터를 전달하느냐에 따라 다른값을 호출되는것

* 유용한 사이트:

JSON Diff checker: http://www.jsondiff.com/ JSON Beautifier/editor: https://jsonbeautifier.org/ JSON Parser: https://jsonparser.org/ JSON Validator: https://tools.learningcontainer.com/json-validator/

댓글남기기