JS推断是否为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"));

    })
})

作者:itmyhome

原文地址:https://www.cnblogs.com/brucemengbm/p/7117884.html