校验是否是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 @ 2019-01-16 15:08  努力的样子  阅读(806)  评论(0编辑  收藏  举报