2013-12-20 113 views
1

我試圖反序列化JSON以下字符串:反序列化JSON複雜

Link to JSON String

{ 
    "result": 1, 
    "error": null, 
    "id": 0, 
    "data": { 
    "ASTEALTHYNODE01_0301_0_30": { 
     "css_class": "sensor rf digital humidity", 
     "default_name": "Humidity", 
     "device_type": "humidity", 
     "did": "30", 
     "gid": "0301", 
     "has_subdevice_count": 0, 
     "has_time_series": 1, 
     "is_actuator": 0, 
     "is_sensor": 1, 
     "is_silent": 0, 
     "last_data": { 
      "DA": 58, 
      "timestamp": 1355791804474 
     }, 
     "meta": {}, 
     "node": "ASTEALTHYNODE01", 
     "shortName": "Humidity", 
     "subDevices": {}, 
     "vid": "0" 
    }, 
    "ASTEALTHYNODE01_0301_0_31": { 
     "css_class": "sensor rf digital temperature", 
     "default_name": "Temperature", 
     "device_type": "temperature", 
     "did": "31", 
     "gid": "0301", 
     "has_subdevice_count": 0, 
     "has_time_series": 1, 
     "is_actuator": 0, 
     "is_sensor": 1, 
     "is_silent": 0, 
     "last_data": { 
      "DA": 26.6, 
      "timestamp": 1355791804475 
     }, 
     "meta": {}, 
     "node": "ASTEALTHYNODE01", 
     "shortName": "Temperature", 
     "subDevices": {}, 
     "vid": "0" 
    }, 
    "ASTEALTHYNODE01_0_0_1000": { 
     "css_class": "actuator cape led rgbled", 
     "default_name": "On Board RGB LED", 
     "device_type": "rgbled", 
     "did": "1000", 
     "gid": "0", 
     "has_subdevice_count": 0, 
     "has_time_series": 0, 
     "is_actuator": 1, 
     "is_sensor": 1, 
     "is_silent": 0, 
     "last_data": { 
      "DA": "22B42B", 
      "timestamp": 1355790209080 
     }, 
     "meta": {}, 
     "node": "ASTEALTHYNODE01", 
     "shortName": "On Board RGB LED", 
     "subDevices": {}, 
     "vid": "0" 
    }, 
    "ASTEALTHYNODE01_0_0_11": { 
     "css_class": "sensor serial rf rf433 receiver transmitter", 
     "default_name": "RF 433Mhz", 
     "device_type": "rf433", 
     "did": "11", 
     "gid": "0", 
     "has_subdevice_count": 1, 
     "has_time_series": 0, 
     "is_actuator": 1, 
     "is_sensor": 1, 
     "is_silent": 0, 
     "last_data": { 
      "DA": "010001010101010100010101", 
      "timestamp": 1355789891324 
     }, 
     "meta": {}, 
     "node": "ASTEALTHYNODE01", 
     "shortName": "RF 433Mhz", 
     "subDevices": { 
      "6l8At": { 
       "category": "rf", 
       "data": "011111110001010100110000", 
       "shortName": "Door Bell", 
       "type": "sensor" 
      } 
     }, 
     "vid": "0" 
    } 
    } 
} 

我通常的幫助下創建類:http://json2csharp.com,然後我做這樣的事情(Json.NET libary): Collapse |複製代碼

var result = JsonConvert.DeserializeObject<MyObject>(jsonString); 

但設備的數量和它們的名字(例如:ASTEALTHYNODE01_0_0_11)是未知之前,我的JSON字符串。我怎樣才能反序列化呢?

謝謝

+0

您是否考慮過創建一個循環,將每個循環發送給代碼隱藏並將反序列化的對象存儲在一個(類型)屬性列表中?不知道你是否可以在視圖狀態下存儲道具 – Lopsided

+0

你是針對.NET框架的最新版本的,這可能是將JSON反序列化爲「動態」對象(「 ''動態結果= JsonConvert.DeserializeObject(jsonString);''') –

+0

@Lopsided很抱歉,但我不明白。你可以寫一些僞代碼還是更詳細地解釋它?謝謝 – user3123001

回答

0

這個工作對我來說,你的JSON,並使用JSON.NET:

 Payload payloadJsonNet = JsonConvert.DeserializeObject<Payload>(data); 
     System.Diagnostics.Debug.Assert 
     (
      payloadJsonNet.data.ContainsKey("ASTEALTHYNODE01_0_0_11") && 
      payloadJsonNet.data["ASTEALTHYNODE01_0_0_11"].subDevices.ContainsKey("6l8At") && 
      payloadJsonNet.data["ASTEALTHYNODE01_0_0_11"].subDevices["6l8At"].shortName == "Door Bell" 
     ); 

...你準備了以下波蘇斯提供(使用漂亮json2csharp幫手,例如):

public class Payload 
    { 
     public int result { get; set; } 
     public int id { get; set; } 
     public Error error { get; set; } 
     public Dictionary<string, Device> data { get; set; } 
    } 

    public class Device 
    { 
     public string css_class { get; set; } 
     public string default_name { get; set; } 
     public string device_type { get; set; } 
     public string did { get; set; } 
     public string gid { get; set; } 
     public int has_subdevice_count { get; set; } 
     public int has_time_series { get; set; } 
     public int is_actuator { get; set; } 
     public int is_sensor { get; set; } 
     public int is_silent { get; set; } 
     public LastData last_data { get; set; } 
     public Meta meta { get; set; } 
     public string node { get; set; } 
     public string shortName { get; set; } 
     public Dictionary<string, Device> subDevices { get; set; } 
     public string vid { get; set; } 
    } 

    public class LastData 
    { 
     public string DA { get; set; } 
     public long timestamp { get; set; } 
    } 

    public class Meta 
    { 
    } 

    public class Error 
    { 
    } 

我想你必須從其他JSON輸入中找出/推斷這些最後兩個傢伙的屬性(即「Meta」和「Error」)。

public class LastData 
    { 
     public string DA { get; set; } 
     public long timestamp { get; set; } 
    } 

最後,只爲好奇,my own parser反序列化精細以及:

 Payload payloadMyParser = new JsonParser().Parse<Payload>(data); 
     System.Diagnostics.Debug.Assert 
     (
      payloadMyParser.data.ContainsKey("ASTEALTHYNODE01_0_0_11") && 
      payloadMyParser.data["ASTEALTHYNODE01_0_0_11"].subDevices.ContainsKey("6l8At") && 
      payloadMyParser.data["ASTEALTHYNODE01_0_0_11"].subDevices["6l8At"].shortName == "Door Bell" 
     ); 

「希望這

還要注意,在使用需要 「長」 型有幫助,

0

您的JSON「數據」屬性看起來像型詞典<串,SomeClass的>,其中SomeClass的財產 - 是

{ 
     "css_class": "sensor rf digital humidity", 
// skipped 
     "subDevices": {}, 
     "vid": "0" 
    }, 

數據結構的一類,所以你可以定義此MyObject類中的屬性,並使用強類型的反序列化沒有任何問題。