源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JS判断是否为JSON对象及是否存在某字段的方法(推荐)

  • 时间:2020-03-23 17:31 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JS判断是否为JSON对象及是否存在某字段的方法(推荐)
[b]实例如下:[/b]
$.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对象及是否存在某字段的方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部