检查json对象中是否存在密钥

问题描述:

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

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

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?

<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"
}

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