[ javascript ] json 사용법
페이지 정보
작성자 웹지기 댓글 0건 조회 3,892회 작성일 21-02-21 01:41본문
JSON
//simplest data interchange format.
//lightweight text-abased structure.
//easy to read.
//key-value pairs.
//used for serialization and transmission of data between the network the network the network connection.
//independent programming language and platform.
//Javascript Object Notation
//1. Object to JSON
//Stringfy(obj)
let json = JSON.stringify(true);
console.log(json);
json = JSON.stringify(['apple', 'banana']);
console.log(json);
const rabbit = {
name:'tori',
color:'white',
size:null,
birthDate:new Date(),
//포암되지 않는 데이터들 simbol function
//symbol:Symbol('id'),
jump:() => {
console.log(`${name} can jump!`);
},
};
json = JSON.stringify(rabbit);
console.log(json);
//해당하는 내용만 json으로 변경가능
json = JSON.stringify(rabbit, ['name', ['size'], ['color']]);
console.log(json);
//더 세밀한 callback을 통해 내용 전달
json = JSON.stringify(rabbit, (key, value) => {
console.log(`key: ${key}, value:${value}`);
//return value;
//더 새밀한 값을 설정
return key === 'name' ? 'test' : value;
});
console.log(json);
//2. JSON to Object
//parse(json)
console.clear();
json = JSON.stringify(rabbit);
console.log(json);
const obj = JSON.parse(json);
const obj2 = JSON.parse(json, (key, value) => {
console.log(`key : ${key}, value : ${value}`);
//return value;
return key === 'bitrhDate' ? new Date(value) : value;
});
console.log(obj);
rabbit.jump();
//obj.jump();
console.log(rabbit.birthDate.getDate());
추천0 비추천0
댓글목록
등록된 댓글이 없습니다.