我試圖解析以下類型JSONArray的:如何判斷我的JSONArray是否包含某個JSONArray?
[{ 「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()的東西嗎?
在執行任何進一步操作之前,只需先獲取'attachmentPoint'並檢查其是否爲null。 'JSONArray attachmentPoint = fldevices.getJSONObject(i).getJSONArray(「attachmentPoint」);如果(attachmentPoint!= null){...}' – Braj
「有沒有類似於.has()的數組」 - 你試過'fldevices.getJSONObject(i).has(「attachmentPoint」)'? – Thomas
謝謝@Braj那完美的工作! – Fama