检查json对象中是否存在密钥 [英] Check if a key exists inside a json object

查看:159
本文介绍了检查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"
}

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

这篇关于检查json对象中是否存在密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆