amt: "10.00"
email: "[email protected]"
merchant_id: "sam"
mobileNo: "9874563210"
orderID: "123456"
passkey: "1234"

上面是我正在处理的JSON对象。我想检查'merchant_id' key 是否存在。我尝试了以下代码,但无法正常工作。有办法实现吗?
<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: "[email protected]",
merchant_id: "sam",
mobileNo: "9874563210",
orderID: "123456",
passkey: "1234"
}

您可以找到详细信息here

10-07 20:50