1

我正在尝试使用 c#、newtonsoft 和 .net 3.5 反序列化下面的 Devices json 结构,但由于“qxdddh”和“q0tL6au”是动态生成的名称,因此无法生成代码。

最终为结构中的每个恒温器创建一个恒温器类的列表或数组(在本例中为“qxdddh”和“q0tL6au”)。

{
    "thermostats": {
        "qxdddh": {
            "locale": "en-US",
            "temperature_scale": "F",
            "is_using_emergency_heat": false,
            "has_fan": true,
            "software_version": "4.1",
            "has_leaf": true,
            "device_id": "qxdddh",
            "name": "",
            "can_heat": true,
            "can_cool": true,
            "hvac_mode": "heat",
            "target_temperature_c": 12.5,
            "target_temperature_f": 55,
            "target_temperature_high_c": 24.0,
            "target_temperature_high_f": 75,
            "target_temperature_low_c": 20.0,
            "target_temperature_low_f": 68,
            "ambient_temperature_c": 21.0,
            "ambient_temperature_f": 70,
            "away_temperature_high_c": 24.0,
            "away_temperature_high_f": 76,
            "away_temperature_low_c": 12.5,
            "away_temperature_low_f": 55,
            "structure_id": "ryWu-tRQstxux0tYhmZ8ESsrGgDjDQ",
            "fan_timer_active": false,
            "name_long": "Thermostat",
            "is_online": true
        },
        "q0tL6au": {
            "locale": "en-US",
            "temperature_scale": "F",
            "is_using_emergency_heat": false,
            "has_fan": true,
            "software_version": "4.1",
            "has_leaf": true,
            "device_id": "q0tL6au",
            "name": "Den",
            "can_heat": false,
            "can_cool": true,
            "hvac_mode": "off",
            "target_temperature_c": 20.5,
            "target_temperature_f": 69,
            "target_temperature_high_c": 24.0,
            "target_temperature_high_f": 75,
            "target_temperature_low_c": 20.0,
            "target_temperature_low_f": 68,
            "ambient_temperature_c": 23.0,
            "ambient_temperature_f": 73,
            "away_temperature_high_c": 24.0,
            "away_temperature_high_f": 76,
            "away_temperature_low_c": 12.5,
            "away_temperature_low_f": 55,
            "structure_id": "ryWu-tqNu0tYhmZ8ESsrGgDjDQ",
            "fan_timer_active": false,
            "name_long": "Den Thermostat",
            "is_online": true
        }
    }
}

代码的最初尝试是

    public class Devices
    {
        public TstatDetails[] thermostats { get; set; }
    }

    public class TstatDetails
    {
         public string locale { get; set; }
         public string temperature_scale { get; set; }
         public string is_using_emergency_heat { get; set; }
         public string has_fan { get; set; }
         public string software_version { get; set; }
         public string has_leaf { get; set; }
         public string device_id { get; set; }
         public string name { get; set; }
         public string can_heat { get; set; }
         public string can_cool { get; set; }
         public string hvac_mode { get; set; }
         public string target_temperature_c { get; set; }
         public string target_temperature_f { get; set; }
         public string target_temperature_high_c { get; set; }
         public string target_temperature_high_f { get; set; }
         public string target_temperature_low_c { get; set; }
         public string target_temperature_low_f { get; set; }
         public string ambient_temperature_c { get; set; }
         public string ambient_temperature_f { get; set; }
         public string away_temperature_high_c { get; set; }
         public string away_temperature_high_f { get; set; }
         public string away_temperature_low_c { get; set; }
         public string away_temperature_low_f { get; set; }
         public string structure_id { get; set; }
         public string fan_timer_active { get; set; }
         public string name_long { get; set; }
         public string is_online { get; set; }
    }

Devices tstats = (Devices) Newtonsoft.Json.JsonConvert.DeserializeObject<Devices>(jsonstring);

产生以下描述的异常

无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型“NestTest.NestOAuth2+TstatDetails[]”,因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。

要修复此错误,要么将 JSON 更改为 JSON 数组(例如 [1,2,3]),要么将反序列化类型更改为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型可以从 JSON 对象反序列化的数组或列表。JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。

我理解错误(我认为),但是对于 c# 来说相当新,我不确定如何纠正这种情况。

4

2 回答 2

2

只需将您的Devices类更改为使用 aDictionary<,>而不是数组:

public class Devices
{
    public Dictionary<string, TstatDetails> thermostats { get; set; }
}

JSON.NET 会将thermostatsJSON 中对象的每个属性解释为字典中的条目,并适当地填充它。(您的调用代码保持不变。)

然后,您将拥有所有按 ID 可用的恒温器。例如:

TstatDetails details = tstats["qxdddh"];

一旦这样做了,我强烈建议您尝试使所有属性名称更传统:)

于 2014-07-08T06:14:53.287 回答
0

您的 Json 对象应该是一个数组,而不仅仅是一个对象。

它基本上应该用[ .... ](其中你肯定可以有多个对象)而不是{ ... }.

它应该看起来类似于:

[
        {
            "locale": "en-US",
            "temperature_scale": "F",
            "is_using_emergency_heat": false,
            ...
        },
        {
            "locale": "en-US",
            "temperature_scale": "F",
            "is_using_emergency_heat": false,
            ...
        }
]

编辑

好吧,我无法与飞碟竞争。但我会留下我的答案,因为它也是正确的(只是解决问题的一种选择)。

于 2014-07-08T06:13:14.003 回答