JS中判断JSON数据是否存在某字段的方法 JavaScript中判断json中是否有某个字段
·
方式一 !("key" in obj)
方式二 obj.hasOwnProperty("key") //obj为json对象。
例如:
var jsonworld_pose = JSON.parse(data[0].world_pose);
var jsonorientation = jsonworld_pose.orientation; //次处可能为undefined
var jsonposition = jsonworld_pose.position;//次处可能为undefined
if (jsonworld_pose.hasOwnProperty("orientation")) {//使用时先进行判断
$("#orientation-w").html(jsonorientation.w);
$("#orientation-x").html(jsonorientation.x);
$("#orientation-y").html(jsonorientation.y);
$("#orientation-z").html(jsonorientation.z);
}
if (jsonworld_pose.hasOwnProperty("position")) {
$("#position-x").html(jsonposition.x);
$("#position-y").html(jsonposition.y);
$("#position-z").html(jsonposition.z);
}转载自https://www.cnblogs.com/cdemo/p/5220685.html
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)