JSON 에 대하여 몇 가지 테스트를 진행.
테스트를 위해 JSON 형태를 더 잘 확인하기 위해서 form 요소의 있는 값을 new form
을 하지 않고
객체에 할당을 해 준다.
const dataSet = {
tel : document.querySelector('input[name=tel]').value,
name : document.querySelector('input[name=name]').value,
id : document.querySelector('input[name=id]').value
}
console.log('Object dataSet 의 값 확인 ===> ', dataSet);
할당된 객체의 데이터를 JSON.stringify() 메서드로 json 문자열로 변환을 한다.
let data = JSON.stringify(dataSet); // json 문자열로 변환
console.log('dataSet 을 JSON 문자열로 변환 ===> ', data);
/** JSON.parse(parameter)
의 정의
*
* JSON.parse 를 통해 파싱이 되는지 확인, Exception 이 발생한다면 json 타입이 아님.
* Converts a JavaScript Object Notation (JSON) string into an object.
* @param text A valid JSON string.
* parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;
* 참고 : JSON cannot be an object. JSON is a string format.
The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.
-> JSON 은 객체가 될 수 없습니다. JSON 의 포멧은 스트링 입니다.
-> 데이터가 문제열 포맷일 때 온전히 JSON 이다.
-> 이것이 자바스크립트 변수로 변환되면 그것은 자바스크립트 Object 이다.
*/
try {
const abc = { }
const testData = JSON.parse(abc);
console.log('JSON Object 가 맞습니다.', typeof testData);
console.log('testData 값 확인 ===> ', testData);
} catch(e) {
console.log('JSON 타입이 아닙니다.');
}
'JavaScript' 카테고리의 다른 글
특정범위에서 클래스 찾기 (0) | 2022.12.09 |
---|---|
[iframe] 자식창에서 부모창으로 데이터 넘기기 (0) | 2022.11.08 |
fetch test v1 (0) | 2022.09.27 |
javaScript 의 Garbage Collector (0) | 2022.09.24 |
fetch 가 ES6 부터 내장으로 들어왔다. (0) | 2022.09.24 |