javascript - 检查 JSON 对象中是否存在键

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

你可以找到详细信息here

https://stackoverflow.com/questions/20804163/

相关文章:

java - 如何遍历 JSONObject?

angularjs - 如何在 AngularJs 中对字典使用 ng-repeat?

java - 仅在序列化期间使用@JsonIgnore,而不是反序列化

json - 未捕获的语法错误意外 token U JSON

java - Gson:直接将String转换为JsonObject(无POJO)

javascript - 如何获取对象长度

javascript - 如何将 json POST 数据作为对象传递给 Web API 方法?

python - 从文件中读取 JSON

javascript - 什么是 $$hashKey 添加到我的 JSON.stringify 结果

json - 如何在 JSON 中转义双引号