JS判断是否为JSON对象及是否存在某字段的方法(推荐)
实例如下:
$.ajax({
type:'POST',
url:url,
success(function(data){
//判断是否为JSON对象
if(typeof(data)=="object"&&
Object.prototype.toString.call(data).toLowerCase()=="[objectobject]"&&!data.length){
alert("isJSON0bject");
}
//判断是否存在某字段
console.info(datas["key"]!=undefined);//此方式不严谨,如果key定义了并就是赋值为undefined则会出问题
console.info("key"indatas);
console.info(datas.hasOwnProperty("key"));
})
})
以上这篇JS判断是否为JSON对象及是否存在某字段的方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。