typeof JSON.parse() == 'object' 当字符串不是json时报错判断不出来,故而用try catch 不报错则为json
代码
/**
* 判断是否json
* @param $string
* @returns {boolean}
*/
function isJson($string)
{
try {
if(typeof JSON.parse($string) == 'object')
return true;
return false;
} catch (e) {
console.log(e);
return false;
}
}