Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JSON从嵌套API反序列化为C#对象_C#_Vb.net_Json.net_Nest Api - Fatal编程技术网

将JSON从嵌套API反序列化为C#对象

将JSON从嵌套API反序列化为C#对象,c#,vb.net,json.net,nest-api,C#,Vb.net,Json.net,Nest Api,正在尝试将我的自定义.NET自动化系统连接到我的Nest恒温器。我在反序列化JSON数据时遇到了一个问题,并没有将所有填充到类中。我对JSON或NEST API不是很精通,所以我希望我能正确地理解这一点。我正在使用Newtonsoft的JSON库 我从顶部开始创建了一个表示所有元数据的类 public class All { public Dictionary<string, Devices> devices { get; set; } public Dictiona

正在尝试将我的自定义.NET自动化系统连接到我的Nest恒温器。我在反序列化JSON数据时遇到了一个问题,并没有将所有填充到类中。我对JSON或NEST API不是很精通,所以我希望我能正确地理解这一点。我正在使用Newtonsoft的JSON库

我从顶部开始创建了一个表示所有元数据的类

public class All
{
    public Dictionary<string, Devices> devices { get; set; }
    public Dictionary<string, Structures> structures { get; set; }
}
一旦我对所有元数据进行了API调用并获取了JSON,我将尝试反序列化

Dim deserializedProduct As Nest.All = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Nest.All)(rawresp)
它不会抛出任何错误,并且似乎处理得很好。就我而言,我只有一个恒温器。运行后,我看到deserializedProduct.devices包含1,deserialize.structures包含1

如果我查看反序列化.structures(0),它具有结构键,并且该值显示为空巢.structure对象,这意味着它没有属性

如果我查看deserializedProduct.devices,它会显示恒温器类,但下面没有其他数据

有人能让这种方法起作用吗?反序列化嵌套JSON是否存在问题


非常感谢您提供的任何帮助或指导。

您没有显示要反序列化的JSON,因此我假设它就是本文中显示的内容

创建类时要记住的事项有:

  • 类中的属性名称需要与JSON中相同级别的属性名称匹配,或者使用
    [JsonProperty]
    属性修饰以指定JSON属性名称
  • 如果JSON属性名称可能不同(即它们是ID),那么此时您需要使用
    字典
    ,其中
    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; }
        }
    
    公共类所有
    {
    公共设备设备{get;set;}
    公共字典结构{get;set;}
    }
    公共类设备
    {
    公共字典恒温器{get;set;}
    }
    公共阶级结构
    {
    公共字符串名称{get;set;}
    公共字符串国家代码{get;set;}
    公共字符串时区{get;set;}
    公共字符串远离{get;set;}
    公共IList恒温器{get;set;}
    公共字符串结构_id{get;set;}
    }
    公共级恒温器
    {
    公共属性{get;set;}
    公共字符串区域设置{get;set;}
    公共字符串温度_刻度{get;set;}
    公共厕所正在使用应急加热{get;set;}
    公共布尔有{get;set;}
    公共字符串软件_版本{get;set;}
    公共布尔有_leaf{get;set;}
    公共字符串设备\u id{get;set;}
    公共字符串名称{get;set;}
    公共布尔可以{get;set;}
    公共布尔可以{get;set;}
    公共字符串hvac_模式{get;set;}
    公共双目标温度{get;set;}
    公共int目标{get;set;}
    公共双目标温度高{get;set;}
    public int target_temperature_high_f{get;set;}
    公共双目标温度低{get;set;}
    public int target_temperature_low_f{get;set;}
    公共双环境温度{get;set;}
    公共int环境温度{get;set;}
    公共双倍温度{get;set;}
    公共int-away\u-temperature\u-high\u-f{get;set;}
    公共双控温度低{get;set;}
    公共int-away\u-temperature\u-low\f{get;set;}
    公共字符串结构_id{get;set;}
    公共bool fan_timer_active{get;set;}
    公共字符串名称_long{get;set;}
    公共布尔是联机的{get;set;}
    公共日期时间上次_连接{get;set;}
    }
    

    Fiddle:

    我不确定它是否有用,因为我不知道Nest API Json格式,但Visual Studio将直接从Json字符串创建类。这将有助于正确匹配格式。嵌套API很复杂。例如,设备可以容纳所有恒温器或保护装置。然后,每个恒温器都有一个可能不同的标识符。请参见下面的剪辑。“EGm9FSC4N7xoc1EHo0CiIXTI3byp3LGD”键是恒温器ID。如果其他人在家中使用此软件,如果我理解正确,他们的恒温器将具有不同的ID。因此,生成类是不可行的。这就是我使用字典方法的原因。也许这也不是最好的办法。“设备”:{“恒温器”:{“EGm9FSC4N7xoc1EHo0CiIXTI3byp3LGD”:{我很想让你所有的属性
    string
    类型,让它工作,然后把它们放回
    bool
    /
    int
    等等。感谢所有的反馈@DavidG.No Dice。我也试着从JSON生成新类,但它仍然没有完全反序列化。可能是JSON.net或我使用的方式有问题g json.net。我将翻阅他们在Stack上的部分,看看是否有人发布了类似的内容。今晚我将尝试一下,Brian,看看它是如何工作的。感谢您的详细回复。实际上,我非常渴望看到这项工作,所以我远程回到家中并尝试了它。感谢@Brian rogers的伟大指导。我肯定是在设计它。没问题很高兴你觉得它有用。
    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; }
    }
    
    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; }
    }
    
    Dim deserializedProduct As Nest.All = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Nest.All)(rawresp)
    
        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; }
        }