[英]Deserializing JSON from Nest API into C# objects
嘗試將我的自定義.NET自動化系統連接到Nest Thermostat。 我遇到了反序列化JSON數據的問題,而不是所有填充到類中的問題。 我不是很熟悉JSON或NEST API,所以我希望我正確地接近這個。 我正在使用Newtonsoft的JSON庫。
我從頂部開始創建了一個代表所有元數據的類。
public class All
{
public Dictionary<string, Devices> devices { get; set; }
public Dictionary<string, Structures> structures { get; set; }
}
然后我創建了Structures Class
public class Structures
{
public Dictionary<string, Structure> structure { get; set; }
}
和設備類
public class Devices
{
public Dictionary<string, Thermostats> thermostats { get; set; }
}
還創建了結構
public class Structure
{
public string name { get; set; }
public string country_code { get; set; }
public string time_zone { get; set; }
public string away { get; set; }
public IList<string> thermostats { get; set; }
public string structure_id { get; set; }
}
和恆溫器
public class Thermostats
{
public Dictionary<string, ThermostatDetails> thermostatdetails { get; set; }
}
最后是Thermostatdetails
public class ThermostatDetails
{
public int humidity { get; set; }
public string locale { get; set; }
public string temperature_scale { get; set; }
public bool is_using_emergency_heat { get; set; }
public bool has_fan { get; set; }
public string software_version { get; set; }
public bool has_leaf { get; set; }
public string device_id { get; set; }
public string name { get; set; }
public bool can_heat { get; set; }
public bool can_cool { get; set; }
public string hvac_mode { get; set; }
public double target_temperature_c { get; set; }
public int target_temperature_f { get; set; }
public double target_temperature_high_c { get; set; }
public int target_temperature_high_f { get; set; }
public double target_temperature_low_c { get; set; }
public int target_temperature_low_f { get; set; }
public double ambient_temperature_c { get; set; }
public int ambient_temperature_f { get; set; }
public double away_temperature_high_c { get; set; }
public int away_temperature_high_f { get; set; }
public double away_temperature_low_c { get; set; }
public int away_temperature_low_f { get; set; }
public string structure_id { get; set; }
public bool fan_timer_active { get; set; }
public string name_long { get; set; }
public bool is_online { get; set; }
public DateTime last_connection { get; set; }
}
一旦我為所有元數據進行API調用並獲得JSON,我就試圖反序列化
Dim deserializedProduct As Nest.All = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Nest.All)(rawresp)
它不會拋出任何錯誤,似乎處理得很好。 在我的情況下,我只有1個恆溫器。 運行后,我看到deserializedProduct.devices包含1,deserialize.structures包含1。
如果我查看deserialize.structures(0),它有結構鍵,值顯示為空的Nest.Structure對象,意味着它沒有屬性。
如果我查看deserializedProduct.devices,它會顯示Thermostats類,但下面沒有其他數據。
有沒有人能夠采用這種方法? 問題是反序列化嵌套的JSON嗎?
非常感謝任何幫助或指導。
您沒有顯示您嘗試反序列化的JSON,因此我將假設它是此API參考頁面上顯示的內容 。
創建類時要記住的事項是:
[JsonProperty]
屬性進行[JsonProperty]
以指定JSON屬性名稱。 Dictionary<string, T>
,其中T
是目標對象類型。 考慮到這一切,你並沒有太遠; 看起來你只是有太多級別的字典,所以結果結構與JSON不匹配。 以下是您需要反序列化設備和結構的所有內容:
public class All
{
public Devices devices { get; set; }
public Dictionary<string, Structure> structures { get; set; }
}
public class Devices
{
public Dictionary<string, Thermostat> thermostats { get; set; }
}
public class Structure
{
public string name { get; set; }
public string country_code { get; set; }
public string time_zone { get; set; }
public string away { get; set; }
public IList<string> thermostats { get; set; }
public string structure_id { get; set; }
}
public class Thermostat
{
public int humidity { get; set; }
public string locale { get; set; }
public string temperature_scale { get; set; }
public bool is_using_emergency_heat { get; set; }
public bool has_fan { get; set; }
public string software_version { get; set; }
public bool has_leaf { get; set; }
public string device_id { get; set; }
public string name { get; set; }
public bool can_heat { get; set; }
public bool can_cool { get; set; }
public string hvac_mode { get; set; }
public double target_temperature_c { get; set; }
public int target_temperature_f { get; set; }
public double target_temperature_high_c { get; set; }
public int target_temperature_high_f { get; set; }
public double target_temperature_low_c { get; set; }
public int target_temperature_low_f { get; set; }
public double ambient_temperature_c { get; set; }
public int ambient_temperature_f { get; set; }
public double away_temperature_high_c { get; set; }
public int away_temperature_high_f { get; set; }
public double away_temperature_low_c { get; set; }
public int away_temperature_low_f { get; set; }
public string structure_id { get; set; }
public bool fan_timer_active { get; set; }
public string name_long { get; set; }
public bool is_online { get; set; }
public DateTime last_connection { get; set; }
}
小提琴: https : //dotnetfiddle.net/7wsRF1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.