Javascript
[javascript] json 데이터 체크
jinist
2022. 1. 27. 05:59
JSON.parse()는 json이 아닌 데이터를 넣으면 에러를 발생시킨다
const json = JSON.parse('hi');
try catch를 이용하여 불러온 json의 데이터가
올바른 json인지 확인할 수 있다.
function IsJsonString(str) {
try {
const json = JSON.parse(str);
return (typeof json === 'object');
} catch (e) {
return false;
}
}