js判断字符串是否为JSON格式
Look:1046,By admin Posted On 2018-03-13
如果JSON.parse能够转换成功;并且转换后的类型为object 且不等于 null,那么这个字符串就是JSON格式的字符串。
function isJSON(str) {
if (typeof str == 'string') {
try {
var obj=JSON.parse(str);
if(typeof obj == 'object' && obj ){
return true;
}
} catch(e) {
console.log('error:'+str+'!!!'+e);
}
return false;
}
console.log('It is not a string!')
}


