判断是否为JSON对象

$.ajax({
    type: 'POST',
    url: url,
    success(function(data){
        //判断是否为JSON对象
        if(typeof(data) == "object" && 
            Object.prototype.toString.call(data).toLowerCase() == "[object object]" && !data.length){
            alert("is JSON 0bject");
        }
        //判断是否存在某字段
        console.info(datas["key"] != undefined); //此方式不严谨,如果key定义了 并就是赋值为undefined 则会出问题
        console.info("key" in datas);
        console.info(datas.hasOwnProperty("key"));

    })
})

 

posted @ 2017-04-14 13:19  dldg  阅读(4014)  评论(0编辑  收藏  举报