jsonpath 判断是否包含_如何判断我的JSONArray是否包含某个JSONArray?

博客讨论了如何在使用JSONPath处理JSON时,检查JSONArray 'attachmentPoint'是否存在,以避免因试图访问不存在的字段而引发异常。建议在访问'attachmentPoint'前先检查其是否为null,或者使用JSONObject的has()方法来验证其存在。
摘要由CSDN通过智能技术生成

[{ “entityClass”: “DefaultEntityClass”, “MAC”:[ “86:2B:A2:F1:2B:图9c”],的 “IPv4”:[ “10.0.0.1”], “IPv6的”:[], “VLAN”:[ “为0x0”], “attachmentPoint”:[{ “switchDPID”:“00:00:00:00:00: 00:00:02" , “端口”:1, “的ErrorStatus”:空}], “lastSeen”:1456312407529},{ “entityClass”: “DefaultEntityClass”, “MAC”:[“1E:94:63:67 :1E:D1 “],” IPv4的 “:[” 10.0.0.3 “],” IPv6的 “:[],” VLAN “:[” 为0x0 “],” attachmentPoint “:[{” switchDPID “:” 00:00 :00:00:00:00:00:03" , “端口”:1, “的ErrorStatus”:空}], “lastSeen”:1456312407625},{ “entityClass”: “DefaultEntityClass”, “MAC”:[” 06:D7:E0:C5:60:86 “],” IPv4的 “:[” 10.0.0.2 “],” IPv6的 “:[],” VLAN “:[” 为0x0 “],” attachmentPoint “:[{” switchDPID “:” 00:00:00:00:00:00:00:02" , “端口”:2 “的ErrorStatus”:空}], “lastSeen”:1456312407591},{ “entityClass”: “DefaultEntityClass” ,“苹果电脑”: [ “6E:C3:E4:5E:1F:65”], “IPv4的”:[ “10.0.0.4”], “IPv6的”:[], “VLAN”:[ “为0x0”], “attachmentPoint”:[ { “switchDPID”: “00:00:00:00:00:00:00:03”, “端口”:2 “的ErrorStatus”:空}], “lastSeen”:1456312407626}]

问题是,有时候会出现“attachmentPoint”JSONArray,有时候没有。如果它不在那里,我会在输出中看到令人讨厌的异常文本。在我尝试运行我的代码之前,如何检查它是否会在那里?

目前,我有以下几点:

if (fldevices.getJSONObject(i).getJSONArray("attachmentPoint").getJSONObject(0).has("switchDPID")

但很明显,这是行不通的,因为它已经尝试访问attachmentPoint,如果它不存在我的错误。对阵列有类似于.has()的东西吗?

2016-03-08

Fama

+1

在执行任何进一步操作之前,只需先获取'attachmentPoint'并检查其是否为null。 'JSONArray attachmentPoint = fldevices.getJSONObject(i).getJSONArray(“attachmentPoint”);如果(attachmentPoint!= null){...}' –

+0

“有没有类似于.has()的数组” - 你试过'fldevices.getJSONObject(i).has(“attachmentPoint”)'? –

+0

谢谢@Braj那完美的工作! –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值