[{ “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那完美的工作! –