检查json对象中是否存在键。

时间:2021-07-09 12:14:04
amt: "10.00"
email: "sam@gmail.com"
merchant_id: "sam"
mobileNo: "9874563210"
orderID: "123456"
passkey: "1234"

The above is the JSON object I'm dealing with. I want to check if the 'merchant_id' key exists. I tried the below code, but it's not working. Any way to achieve it?

上面是我要处理的JSON对象。我想检查一下“商人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>

6 个解决方案

#1


383  

Try this,

试试这个,

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

}

the JS Object thisSession should be like

这个会话的JS对象应该是这样的。

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

you can find the details here

你可以在这里找到细节。

#2


54  

There's several ways to do it, depending on your intent.

有几种方法,取决于你的意图。

thisSession.hasOwnProperty('merchant_id'); will tell you if thisSession has that key itself (i.e. not something it inherits from elsewhere)

thisSession.hasOwnProperty(“merchant_id”);会告诉你这个会话是否有那个键本身(也就是说它不是从其他地方继承的)

"merchant_id" in thisSession will tell you if thisSession has the key at all, regardless of where it got it.

在这个会话中,“商人id”会告诉你这个会话是否有钥匙,不管它在哪里。

thisSession["merchant_id"] will return false if the key does not exist, or if its value evaluates to false for any reason (e.g. if it's a literal false or the integer 0 and so on).

如果密钥不存在,或者它的值因任何原因被计算为false(例如,如果它是一个文本错误或整数0等等),那么这个会话将返回false。

#3


5  

Type check will also work as :

类型检查也可作为:

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

#4


3  

I just wanted to point this out even though I'm late to the party. The original question you were trying to find a 'Not IN' essentially which looks like is not supported from the research (2 links below) that I was doing. So if you wanted to do a not:

我只是想指出这一点,尽管我已经迟到了。你试图找到一个“不存在”的原始问题,在我正在做的研究(下面2个链接)中似乎没有得到支持。所以如果你想做一个不是:

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

I'd recommend just setting that expression == to what you're looking for

我建议你只设置这个表达式==你要找的东西。

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

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

#5


0  

function to check undefined and null objects

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

here is an easy way to check whether object sent is contain undefined or 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");
)

#6


-10  

You can try if(typeof object !== 'undefined')

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

#1


383  

Try this,

试试这个,

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

}

the JS Object thisSession should be like

这个会话的JS对象应该是这样的。

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

you can find the details here

你可以在这里找到细节。

#2


54  

There's several ways to do it, depending on your intent.

有几种方法,取决于你的意图。

thisSession.hasOwnProperty('merchant_id'); will tell you if thisSession has that key itself (i.e. not something it inherits from elsewhere)

thisSession.hasOwnProperty(“merchant_id”);会告诉你这个会话是否有那个键本身(也就是说它不是从其他地方继承的)

"merchant_id" in thisSession will tell you if thisSession has the key at all, regardless of where it got it.

在这个会话中,“商人id”会告诉你这个会话是否有钥匙,不管它在哪里。

thisSession["merchant_id"] will return false if the key does not exist, or if its value evaluates to false for any reason (e.g. if it's a literal false or the integer 0 and so on).

如果密钥不存在,或者它的值因任何原因被计算为false(例如,如果它是一个文本错误或整数0等等),那么这个会话将返回false。

#3


5  

Type check will also work as :

类型检查也可作为:

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

#4


3  

I just wanted to point this out even though I'm late to the party. The original question you were trying to find a 'Not IN' essentially which looks like is not supported from the research (2 links below) that I was doing. So if you wanted to do a not:

我只是想指出这一点,尽管我已经迟到了。你试图找到一个“不存在”的原始问题,在我正在做的研究(下面2个链接)中似乎没有得到支持。所以如果你想做一个不是:

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

I'd recommend just setting that expression == to what you're looking for

我建议你只设置这个表达式==你要找的东西。

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

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

#5


0  

function to check undefined and null objects

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

here is an easy way to check whether object sent is contain undefined or 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");
)

#6


-10  

You can try if(typeof object !== 'undefined')

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