JS 기초 강의(ES5+) - # JSON (10)

Minsoo·2021년 8월 20일
0

JSON: JavaScript Object Notation

  • simplest data interchange format
  • lightweight text-based structure
  • easy to read
  • key-value pairs
  • used for serialization(직렬화) and transmission of data between the network the network connection
  • independent programming language and platform (중요한 특징)
  • [object] -> serialize -> [string]
  • [object] <- deserialize <- [string]

오버로딩(Overloading): 함수 이름은 같지만, 어떤 파라미터를 전달하느냐에 따라서 각각 다른 방식으로 호출이 가능. (ex. JSON.stringify)

1. Object to JSON

  • stringify(obj) 문자열 변환
let json = JSON.stringify(true);
console.log(json);

true

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

["apple","banana"]

  • "symbol"과 같은 JS의 특별한 데이터는 json에 포함되지 않음.
const rabbit = {
  name: 'tori',
  color: 'white',
  size: null,
  birthDate: new Date(),
  jump: function () {
    console.log(`${this.name} can jump!`);
  },
};

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

{"name":"tori","color":"white","size":null,"birthDate":"2021-08-31T03:34:44.240Z"}

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

{"name":"tori","color":"white","size":null}

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

key: , value: [object Object]
key: name, value: tori
key: color, value: white
key: size, value: null
key: birthDate, value: 2021-08-31T03:34:44.240Z
key: jump, value: function () {
console.log(${this.name} can jump!);
}
{"name":"ellie","color":"white","size":null,"birthDate":"2021-08-31T03:34:44.240Z"}

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

{"name":"tori","color":"white","size":null,"birthDate":"2021-08-31T03:34:44.240Z"}

코드를 입력하세요

1개의 댓글

comment-user-thumbnail
2021년 8월 20일

제너레이터 + 이터레이터 + 이터러블

답글 달기

관련 채용 정보