本文翻译自:Check if a key exists inside a json object

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

The above is the JSON object I'm dealing with. 上面是我正在处理的JSON对象。 I want to check if the 'merchant_id' key exists. 我想检查'merchant_id'密钥是否存在。 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>

#1楼

参考:https://stackoom.com/question/1PI71/检查密钥是否存在于json对象中


#2楼

您可以尝试if(typeof object !== 'undefined')


#3楼

Try this, 尝试这个,

if(thisSession.hasOwnProperty('merchant_id')){}

the JS Object thisSession should be like JS对象thisSession应该像

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

you can find the details here 您可以在这里找到详细信息


#4楼

There's several ways to do it, depending on your intent. 有多种方法可以完成此操作,具体取决于您的意图。

thisSession.hasOwnProperty('merchant_id'); will tell you if thisSession has that key itself (ie not something it inherits from elsewhere) 会告诉您thisSession是否具有该密钥本身(即不是它从其他地方继承来的)

"merchant_id" in thisSession will tell you if thisSession has the key at all, regardless of where it got it. "merchant_id" in thisSession将告诉您thisSession是否完全具有密钥,无论它从何处获取。

thisSession["merchant_id"] will return false if the key does not exist, or if its value evaluates to false for any reason (eg if it's a literal false or the integer 0 and so on). 如果键不存在,或者键的值由于任何原因(例如,它是字面值false或整数0,依此类推),则thisSession["merchant_id"]将返回false。


#5楼

Type check also works : 类型检查也可以:

if(typeof Obj.property == "undefined"){// Assign value to the property hereObj.property = someValue;
}

#6楼

(I wanted to point this out even though I'm late to the party) (即使我迟到,我也想指出这一点)
The original question you were trying to find a 'Not IN' essentially. 您试图从本质上查找“ Not IN”的原始问题。 It looks like is not supported from the research (2 links below) that I was doing. 我正在做的研究似乎不支持(下面2个链接)。

So if you wanted to do a 'Not In': 因此,如果您想执行“不参加”活动:

("merchant_id" in x)
true
("merchant_id_NotInObject" in x)
false

I'd recommend just setting that expression == to what you're looking for 我建议只将表达式==设置为您要查找的内容

if (("merchant_id" in thisSession)==false)
{// do nothing.
}
else
{alert("yeah");
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in http://www.w3schools.com/jsref/jsref_operators.asp https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/in http://www.w3schools.com/jsref/jsref_operators.asp

检查密钥是否存在于json对象中相关推荐

  1. 检查键是否存在于JavaScript对象中?

    如何检查JavaScript对象或数组中是否存在特定键? 如果密钥不存在,而我尝试访问它,它将返回false吗? 还是抛出错误? #1楼 它将返回undefined . var aa = {hello ...

  2. XE3随笔6:SuperObject 的 JSON 对象中还可以包含 方法

    SuperObject 的 JSON 对象中还可以包含 "方法", 这太有意思了; 其方法的格式是: procedure Method(const This, Params: IS ...

  3. JSON对象中的函数调用,JSON格式的字符串对应的函数调用方法

    json对象中的函数调用:直接使用 json变量名+函数名+()进行调用,特别注意这对括号:如果是获取到的json格式的字符串,想要调用它的方法前,需要先将该字符串转换成json对象,在调用对应的方法 ...

  4. 如何替换json对象中的key

    来源 | https://www.cnblogs.com/jaxu/archive/2021/06/01/14836088.html 看到标题你可能会想,如此简单的问题值得去探究吗?如果我有一个jso ...

  5. JS-JSON-获取JSON对象中的数据展示到表格里

    JS-JSON-获取JSON对象中的数据展示到表格里 1.要求 点击按钮,将学生信息展示到表格里,并显示总记录条数. 2.实现思路 创建JSON对象 先创建一个JSON对象data,有总人数total ...

  6. 移除Java对象中的属性_移除/添加 JSON对象中的某个属性

    因为JSON完全是动态的,所以给一个JSON对象添加一个属性是很简单的,如: var json = {}; json.username='xtayfjpk'; 这样就为json对象添加了一个名为use ...

  7. JS 遍历JSON对象中的键值对

    对象:一组无序属性的集合,属性的值可以是任意的类型: json也是对象,数据都是成对的,也就是键值对: json实际上就是一组格式化后的字符串数据. 遍历JSON对象中的数据,可通过for-in循环实 ...

  8. 将JSON对象中的某个字段进行分组和排序(java实现)

    现在有这样一个数据 { "systemid": "123", "productid": "123", "dat ...

  9. 获取Json对象中Json数组中的一个Json对象

    比如:获取以下数据的data中的数据 { "message": "successful", "resultCode": "0&qu ...

最新文章

  1. Android中通过自定义签名控件实现手写签名
  2. Ubuntu 16.04添加启动图标到Dash Home中
  3. 一些常用的辅助代码 (网络收藏)
  4. 配置python虚拟环境心得
  5. Android 应用程序之间数据共享—ContentProvider
  6. oracle 判断最后一笔_美股道琼斯日线杀跌一笔结束,向上一笔的反弹顺理成章。又到金秋弯腰拾金时...
  7. eclipse反编译调试源码调试
  8. 项目二-成员函数、友元函数和一般函数之区别
  9. 回顾日本和台湾的股市泡沫
  10. 带哨兵的冒泡排序_冒泡排序(Bubble Sort)
  11. cos三次方积分_cosx的四次方如何积分?
  12. POST请求和PUT请求的区别
  13. association判断不为空时_mybatis 一对一关联 association 返回空值
  14. RK3399平台开发系列讲解(内核驱动外设篇)6.5、音频芯片ES8323 基础知识及设备树相关配置
  15. word如何设置上标形式_word如何将107中的7设置为上标表示形式
  16. Endnote中有的文献没有Pages信息怎么办?
  17. vmware worlstation15.5 打开虚拟机报错 :operation inconsistent with current state!
  18. 【PS】photoshop怎么添加层遮罩?
  19. 【字节跳动】头条竞价广告|商业产品经理实习生|一面凉经
  20. 45种可以获得Webshell的程序[转]

热门文章

  1. RecycleView 嵌套 RecycleView 导致自动滚动
  2. android DataBind LiveData ViewModel 使用详解
  3. 编写你的第一个 Flutter App
  4. Android Studio 新建项目的R文件丢失的解决方法
  5. Android之对Volley网络框架的一些理解
  6. Java中finally和return执行顺序
  7. IOS单例的两种实现方式
  8. Hadoop(十五)MapReduce程序实例
  9. 【bzoj3771】Triple FFT+容斥原理
  10. 通过crontab命令创建任务