JSON.stringify() : 객체 ㅡ> Json 문자열(string자료형)
JSON.parse() : Json문자열 ㅡ> 객체(object 자료형)
//10.14과제
const jsObject = [{
name: 'lee',
song: 'good'
},{
name: 'park',
song: 'withyou'
}];
const outputA = JSON.stringify(jsObject)
//json문자열로 값을 변환(객체 등으로 표현된 데이터를 문자열로 다루기 쉽게끔함)
const outputB = JSON.stringify(jsObject, null, 2);//2칸만큼 들여쓰기
console.log(typeof(outputA)) //string
console.log(outputA) // [{"name":"lee","song":"good"},{"name":"park","song":"withyou"}]
console.log('------')
console.log(outputB)
//출력내용
// [
// {
// "name": "lee",
// "song": "good"
// },
// {
// "name": "park",
// "song": "withyou"
// }
// ]
console.log('----------')
const outputC = JSON.parse(outputB) // Json 문자열을 js객체로 변경
console.log(typeof(outputC)) // object
console.log(outputC) // [ { name: 'lee', song: 'good' }, { name: 'park', song: 'withyou' } ]
'🗃️javascript > 이론정리' 카테고리의 다른 글
js의 this 반복정리 (0) | 2024.01.05 |
---|---|
jquery 기본예시들 정리 (0) | 2023.12.14 |
섀도잉[이름 동일하면 범위 무관 값동일] || 콜백함수[객체지향의 기운이..] (0) | 2023.10.25 |
setTimeout, setInterval || 호이스팅[선언적 함수는 맨위로 끌어올려짐.] (0) | 2023.10.25 |
JavaScript에서 숫자 * undefined = NaN (0) | 2023.10.07 |