function isJSON(str) {
if (typeof str == 'string') {
try { //try{}catch(e){} 使用是因为 JSON.parse(str) 不满足是报错 通过其抛异常 return false
var obj = JSON.parse(str);
if (typeof JSON.parse(str) == 'object') {
console.log(111);
return true;
} else {
console.log(22);
return false;
}
} catch (e) {
console.log(333);
console.log(e);
return false;
}
}
console.log(444);
return false;
}
console.log(isJSON('{"code":200,}'));
延伸:若有需要某些判断且判断条件不满足 是 js 报错 可使用try{}catch(e){} 抛异常进行判断 条件是否符合.

条件不满足:

条件满足:
