Front-end/Javascript
JavaScript-특정 문자열이 JSON 인지 확인 / 파악하기
조미남
2013. 7. 24. 14:31
- 특정 문자열이 JSON 문자열 형식인지 파악하기
- 이것은 어떠한 데이터를 JSON Object 로 변환하기 전 확인할 때 사용할 수 있습니다.
var response = false; try { response = jQuery.parseJSON('response from server'); } catch (error) { console.log("is not JSON type String or else); } if(respone && typeof response =='object') { //It is JSON } else { if(response === false || response == null) { //the response was a string "false", parseJSON will convert it to boolean false } else { //the response was something else } }
참고: http://stackoverflow.com/questions/4295386/how-can-i-check-if-a-value-is-a-json-object