繁体   English   中英

检查 JSON 对象中是否存在键

[英]Check if a key exists inside a JSON object

amt: "10.00"
email: "sam@gmail.com"
merchant_id: "sam"
mobileNo: "9874563210"
orderID: "123456"
passkey: "1234"

以上是我正在处理的 JSON 对象。 我想检查merchant_id键是否存在。 我尝试了下面的代码,但它不起作用。 有什么方法可以实现吗?

<script>
window.onload = function getApp()
{
  var thisSession = JSON.parse('<?php echo json_encode($_POST); ?>');
  //console.log(thisSession);
  if (!("merchant_id" in thisSession)==0)
  {
    // do nothing.
  }
  else 
  {
    alert("yeah");
  }
}
</script>

试试这个,

if(thisSession.hasOwnProperty('merchant_id')){

}

JS 对象thisSession应该像

{
amt: "10.00",
email: "sam@gmail.com",
merchant_id: "sam",
mobileNo: "9874563210",
orderID: "123456",
passkey: "1234"
}

你可以在这里找到详细信息

有几种方法可以做到这一点,这取决于您的意图。

thisSession.hasOwnProperty('merchant_id'); 会告诉您 thisSession 本身是否具有该密钥(即不是从其他地方继承的)

"merchant_id" in thisSession会告诉你 thisSession 是否有密钥,不管它是从哪里得到的。

thisSession["merchant_id"]如果键不存在,或者它的值由于任何原因评估为假(例如,如果它是一个文字false或整数 0 等等),将返回假。

(我想指出这一点,即使我参加聚会迟到了)
您本质上试图找到“不在”的原始问题。 我正在做的研究(下面的 2 个链接)似乎不支持。

所以如果你想做一个“不在”:

("merchant_id" in x)
true
("merchant_id_NotInObject" in x)
false 

我建议只将该表达式 == 设置为您要查找的内容

if (("merchant_id" in thisSession)==false)
{
    // do nothing.
}
else 
{
    alert("yeah");
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in http://www.w3schools.com/jsref/jsref_operators.asp

你可以这样做:

if("merchant_id" in thisSession){ /** will return true if exist */
 console.log('Exist!');
}

if(thisSession["merchant_id"]){ /** will return its value if exist */
 console.log('Exist!');
}

类型检查也有效:

if(typeof Obj.property == "undefined"){
    // Assign value to the property here
    Obj.property = someValue;
}

此代码导致 esLint 问题: no-prototype-builtins builtins

foo.hasOwnProperty("bar") 

这里的建议方法是:

Object.prototype.hasOwnProperty.call(foo, "bar");

我稍微改变了你的 if 语句并且有效(也适用于继承的 obj - 查看代码段)

if(!("merchant_id" in thisSession)) alert("yeah");

 var sessionA = { amt: "10.00", email: "sam@gmail.com", merchant_id: "sam", mobileNo: "9874563210", orderID: "123456", passkey: "1234", } var sessionB = { amt: "10.00", email: "sam@gmail.com", mobileNo: "9874563210", orderID: "123456", passkey: "1234", } var sessionCfromA = Object.create(sessionA); // inheritance sessionCfromA.name = 'john'; if (!("merchant_id" in sessionA)) alert("merchant_id not in sessionA"); if (!("merchant_id" in sessionB)) alert("merchant_id not in sessionB"); if (!("merchant_id" in sessionCfromA)) alert("merchant_id not in sessionCfromA"); if ("merchant_id" in sessionA) alert("merchant_id in sessionA"); if ("merchant_id" in sessionB) alert("merchant_id in sessionB"); if ("merchant_id" in sessionCfromA) alert("merchant_id in sessionCfromA");

检查未定义和空对象的函数

function elementCheck(objarray, callback) {
        var list_undefined = "";
        async.forEachOf(objarray, function (item, key, next_key) {
            console.log("item----->", item);
            console.log("key----->", key);
            if (item == undefined || item == '') {
                list_undefined = list_undefined + "" + key + "!!  ";
                next_key(null);
            } else {
                next_key(null);
            }
        }, function (next_key) {
            callback(list_undefined);
        })
    }

这是检查发送的对象是否包含 undefined 或 null 的简单方法

var objarray={
"passenger_id":"59b64a2ad328b62e41f9050d",
"started_ride":"1",
"bus_id":"59b8f920e6f7b87b855393ca",
"route_id":"59b1333c36a6c342e132f5d5",
"start_location":"",
"stop_location":""
}
elementCheck(objarray,function(list){
console.log("list");
)

你可以试试if(typeof object !== 'undefined')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM