反序列化复杂的 JSON



我正在尝试反序列化以下 JSON 字符串:

链接到 JSON 字符串

{
  "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 库):折叠 |复制代码

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

但是在我获得 JSON 字符串之前,设备的数量及其名称(例如:ASTEALTHYNODE01_0_0_11)是未知的。我怎样才能反序列化这个?

谢谢

这对

我有用,使用您的 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"
        );

。前提是您准备了以下 POCO(例如,使用 Nifty 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 输入中找出/推断最后两个家伙的属性应该是什么(即"元"和"错误")。

另请注意,需要在以下方面使用的"长"类型:

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

最后,只是为了好奇,我自己的解析器也可以很好地反序列化它:

        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"
        );

"希望这有帮助,

您的 json "data" 属性看起来像类型为 Dictionary <字符串 SomeClass="> 的属性,其中 SomeClass - 是一个类

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

数据结构,因此您可以在 MyObject 类中定义此属性并使用强类型化反序列化,没有任何问题。

相关内容

  • 没有找到相关文章

最新更新