如题所述
第1个回答 2019-10-11
实例如下:
$.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"));
})
})
以上这篇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"));
})
})
以上这篇JS判断是否为JSON对象及是否存在某字段的方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。