0
点赞
收藏
分享

微信扫一扫

js判断是否是Json对象


有两种方法

1.使用正则表达式(推荐),代码如下:

if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {

//the json is ok

}else{

//the json is not ok

}

2.使用try-catch

function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}


举报

相关推荐

0 条评论