我正在尝试使用c#,newtonsoft和.net 3.5对下面的设备json结构进行反序列化,因此put无法生成可用作" 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对象(例如{&#34; name&#34;:&#34; value&#34;}) 进入类型&#39; NestTest.NestOAuth2 + TstatDetails []&#39;因为类型 需要一个JSON数组(例如[1,2,3])才能正确反序列化。
要修复此错误,请将JSON更改为JSON数组(例如 [1,2,3])或更改反序列化类型,使其成为正常的.NET type(例如,不是像整数这样的基本类型,不是集合类型 像数组或List一样,可以从JSON对象反序列化。 JsonObjectAttribute也可以添加到类型中以强制它 从JSON对象反序列化。
我理解错误(我认为),但对c#相当新,我不确定如何纠正这种情况。
答案 0 :(得分:2)
只需更改Devices
课程即可使用Dictionary<,>
代替数组:
public class Devices
{
public Dictionary<string, TstatDetails> thermostats { get; set; }
}
JSON.NET会将JSON中thermostats
对象的每个属性解释为字典中的条目,并相应地填充它。 (您的主叫代码保持不变。)
然后您将通过ID获得所有恒温器。例如:
TstatDetails details = tstats["qxdddh"];
一旦有效,我强烈建议您尝试使所有属性名称更加传统:)
答案 1 :(得分:0)
你的Json对象应该是一个数组,而不仅仅是一个对象。
它基本上应该包含[ .... ]
(其中你肯定有多个对象),而不是{ ... }
。
它可能看起来与此相似:
[
{
"locale": "en-US",
"temperature_scale": "F",
"is_using_emergency_heat": false,
...
},
{
"locale": "en-US",
"temperature_scale": "F",
"is_using_emergency_heat": false,
...
}
]
修改强>
好吧,我无法与The Skeet竞争。 但我会留下我的答案,因为它也是正确的(只有一个选项来解决问题)。