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

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

转载: https://www.cnblogs.com/lanleiming/p/7096973.html

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 on 2021-08-17 16:39  梦幻飞雪  阅读(545)  评论(0编辑  收藏  举报