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!')
}
posted @ 2018-08-27 16:01  007少侠  阅读(350)  评论(0编辑  收藏  举报
友情链接:便宜vps(CN2GIA线路)
站长统计: