JSON stringify, parse

 JSON.stringify(object) : 객체를 JSON양식의 문자열로 변형한다.


let obj = {
apple : 1,
banana : 2,
tomato : 3,
orange : 4
}

let str = JSON.stringify(obj);

console.log(str);


위 str의 콘솔로그 결과는 아래와 같이 문자열(string)로 출력된다.

{

    "apple" : 1,

    "banana" : 2,

    "tomato" : 3,

    "orange" : 4

}


 JSON.parse(str) : JSON양식의 문자열을 JSON양식의 객체(object)로 변형한다.


let str = '{"apple" : 1, "banana" : 2, "tomato" : 3, "orange" : 4}';


let obj = JSON.parse(str);

console.log(obj);


str은 위쪽의 콘솔로그에 { apple: 1, banana: 2, tomato: 3, orange: 4 }와 같이 출력이 된다.


 

댓글

이 블로그의 인기 게시물

Lesson 12_1 프로퍼티 노드(Property Node)

DAQ로 전압 측정하기-2

Lesson 12_2 참조를 이용한 프로퍼티노드(Property Node)