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

标签 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

关于javascript - 检查 JSON 对象中是否存在键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20804163/

相关文章:

java - React JS中如何将JSON数据转换为XML格式数据并下载文件

c# - 解析 Azure API 管理订阅数据的 json 失败

java - 在气球内显示带有照片的多个地标的最佳做法是什么?

javascript - jQuery 动画滚动无需 $.browser 并且不会触发两次

php - 将值从 php 数组传输到 jquery

php - 使用 JSON 插入时出现问题

ios - 来自 nsmanagedObject 的 NSJSONSerialization 表示键不符合键值编码

javascript - Angular JS : Unordered List Elements not getting displayed

javascript - 如何将 scala Seq[] 转换为 Javascript 数组?

Javascript 连接到 joomla mysql 数据库