js判断字符串是否为JSON格式

function isJson(str) {
if (typeof str == 'string') {
try {
var obj=JSON.parse(str);
if(typeof obj == 'object' && obj ){
return true;
}else{
return false;
}
} catch(e) {
console.log('error:'+str+'!!!'+e);
return false;
}
}
console.log('It is not a string!')
}

猜你喜欢

转载自blog.csdn.net/dream1120757048/article/details/79139058