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);
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM