[ javascript ] json 사용법 > javascript&jQuery

본문 바로가기
사이트 내 전체검색

javascript&jQuery

[ javascript ] json 사용법

작성자 웹지기
작성일 21-02-21 01:41 | 조회 8,100 | 댓글 0

본문

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

댓글목록 0

등록된 댓글이 없습니다.

javascript&jQuery 목록

Total 63
게시물 검색

회원로그인

접속자집계

오늘
3,066
어제
36,187
최대
61,067
전체
18,070,243

그누보드5
Copyright © funyphp.com. All rights reserved.