2014-12-04 197 views
-1

我有一個大的JSON對象,當我要搜索的JSON對象特定的節點,我得到nullpointerexception因爲空不存在,我的JSON是這樣搜索節點

"InternetGatewayDevice": { 
    "DeviceSummary": { 
    "_value": "InternetGatewayDevice:1.1[](Baseline:1, DeviceAssociation:1, Time:1, QoS:1, Bridging:1, IPPing:1, USBLAN:1, WiFiLAN:1, GponWAN:1), VoiceService:1.0[1](Endpoint:1, SIPEndpoint:1)", 
    "_timestamp": "2014-12-01T09:07:09.943Z", 
    "_type": "xsd:string" 
    }, 
    "DeviceInfo": { 
    "SpecVersion": { 
     "_value": "1.0", 
     "_timestamp": "2014-12-01T09:07:09.943Z", 
     "_type": "xsd:string" 
    }, 
    "HardwareVersion": { 
     "_value": "V1.0", 
     "_timestamp": "2014-12-01T09:07:09.943Z", 
     "_type": "xsd:string" 
    }, 
    "SoftwareVersion": { 
     "_value": "V1.1", 
     "_timestamp": "2014-12-01T09:07:09.943Z", 
     "_type": "xsd:string" 
    }, 
    "ProvisioningCode": { 
     "_value": "", 
     "_timestamp": "2014-12-01T09:07:09.943Z", 
     "_type": "xsd:string" 
    } 
    }, 
    "ManagementServer": { 
    "ConnectionRequestURL": { 
     "_value": "xxxxxx", 
     "_timestamp": "2014-12-01T09:07:09.943Z", 
     "_type": "xsd:string" 
    }, 
    "ParameterKey": { 
     "_value": "", 
     "_timestamp": "2014-12-01T09:07:09.943Z", 
     "_type": "xsd:string" 
    } 
    }, 
    "WANDevice": { 
    "1": { 
     "WANConnectionDevice": { 
     "10": { 
      "WANPPPConnection": { 
      "1": { 
       "ExternalIPAddress": { 
       "_value": "xxxxxx", 
       "_timestamp": "2014-12-01T09:07:09.943Z", 
       "_type": "xsd:string" 
       }, 
       "Username": { 
       "_value": "xxxxxxxx", 
       "_timestamp": "2014-12-01T09:07:09.943Z", 
       "_type": "xsd:string" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

我尋找這個LANDevice和我使用此代碼搜索節點::

JArray deviceJArray = JArray.Parse(jsonResult); 
var strAuthModeBasic = deviceJArray[0]["InternetGatewayDevice"]["LANDevice"]["InternetGatewayDevice"]["LANDevice"]["1"]["WLANConfiguration"]["1"]["BeaconType"]["_value"].ToString(); 

,我想處理這個異常請。

+0

你問在數據丟失時該怎麼辦,或者即使對於使用有效數據的json,你也總是得到這個異常? – 2014-12-04 09:23:38

+0

當我錯過了數據。 – danarj 2014-12-04 09:24:32

+1

是否知道'try ... catch'關鍵字? – 2014-12-04 09:25:13

回答

1

我會通過將json序列化爲一個c#複雜對象來執行此操作,您可以在其中進行空值檢查。你得到錯誤的原因是因爲你正在給變量賦值一個不存在的值。

這是系列化的微軟的例子,如果它是從一個服務:http://msdn.microsoft.com/en-us/library/bb412179%28v=vs.110%29.aspx

,或者如果你只是想序列化JSON字符串你已經有,使用javascriptserializer: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx

0

我用東西simillar,但在JS。

我還沒有測試過,介意你。基本上:遍歷對象,當遇到缺少的步驟時返回null。如果您設法找到您的實際主對象,則返回所需的值。

var path = "path.to.your.object"; // instead of array["path"]["to"]["your"]["object"] 
var fieldName = "_value"; 
array.Get(path, fieldName); 

public static string Get(this JArray a, string path, string fieldName){ 
    // isnullorempty path -> return null 
    var steps = path.Split("."); 
    JArray obj = a; 
    for(var i = 0; i < steps.length; ++i){ 
     obj = obj[steps[i]]; 
     if(obj == null) return null; 
    } 
    return obj[fieldName]; 
}