【问题标题】:Deserializing this JSON response to C#将此 JSON 响应反序列化到 C#
【发布时间】:2015-03-31 19:05:58
【问题描述】:

我有这个特定的 JSON 响应,我试图反序列化但没有成功。我希望有人可以帮助我。

这是我得到的 JSON 响应:

{
"num_locations": 1,
"locations": {
    "98765": {
        "street1": "123 Fake Street",
        "street2": "",
        "city": "Lawrence",
        "state": "Kansas",
        "postal_code": "66044",
        "s_status": "20",
        "system_state": "Off"
    }
}

}

我使用了 json2csharp http://json2csharp.com 并得到了这些推荐的类:

    public class __invalid_type__98765
    {
        public string street1 { get; set; }
        public string street2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string postal_code { get; set; }
        public string s_status { get; set; }
        public string system_state { get; set; }
    }

    public class Locations
    {
        public __invalid_type__98765 __invalid_name__98765 { get; set; }
    }

    public class RootObject
    {
        public int num_locations { get; set; }
        public Locations locations { get; set; }
    }

但是当我尝试在我的代码中使用它时:

var locationResponse = JsonConvert.DeserializeObject<RootObject>(response.Content);

我得到的是(观看):

locationResponse : {RestSharpConsoleApplication.Program.RootObject} : RestSharpConsoleApplication.Program.RootObject
locations : {RestSharpConsoleApplication.Program.Locations} : RestSharpConsoleApplication.Program.Locations
__invalid_name__98765 : null : RestSharpConsoleApplication.Program.__invalid_type__98765
num_locations : 1 : int

显然我没有为 DeserializeObject 创建 (json2csharp) 正确的类,遗憾的是我无法控制 JSON 响应 (vendor = SimpliSafe)。

很明显,“98765”是一个值(位置编号),但 json2csharp 将它放入这个 __invalid_type__98765 类中,这可能就是它变为 null 的原因。

知道类应该如何查找这个特定的 JSON 以成功反序列化吗?

谢谢! 扎克斯

【问题讨论】:

    标签: c# json serialization json2csharp


    【解决方案1】:

    你应该可以用字典来做到这一点:

    public class MyData{ 
      [JsonProperty("locations")]
      public Dictionary<string, Location> Locations {get;set;} 
    }
    public class Location
    {
      public string street1 { get; set; }
      public string street2 { get; set; }
      public string city { get; set; }
      public string state { get; set; }
      public string postal_code { get; set; }
      public string s_status { get; set; }
      public string system_state { get; set; }
    }
    

    【讨论】:

      【解决方案2】:

      您是否有非 Express 版本的 Visual Studio?如果是这样,请将 JSON 复制到剪贴板,然后转到 Visual Studio 菜单:编辑 >> 特殊粘贴 >> 将 JSON 粘贴为类。

      使用它给出:

      public class Rootobject {
          public int num_locations { get; set; }
          public Locations locations { get; set; }
      }
      
      public class Locations {
          public _98765 _98765 { get; set; }
      }
      
      public class _98765 {
          public string street1 { get; set; }
          public string street2 { get; set; }
          public string city { get; set; }
          public string state { get; set; }
          public string postal_code { get; set; }
          public string s_status { get; set; }
          public string system_state { get; set; }
      }
      

      这表明您的 JSON 结构不太正确。

      【讨论】:

      • 这(除了_98765类的名称)与json2csharp生成的OPs类结构完全相同。 JSON 很好,但这只是 JSON“模式”中的一个示例,我们必须使用一些常识来推断自己(即注意可能有多个位置,并且值 '98765' 只是一个标识符值与其中之一相关联)。
      【解决方案3】:

      您还可以通过属性指定属性名称来解决此问题:

      public class RootObject
      {
          public int num_locations { get; set; }
          public Locations locations { get; set; }
      }
      
      public class Locations
      {
          [JsonProperty("98765")]
          public LocationInner Inner { get; set; }
      }
      
      public class LocationInner
      {
          public string street1 { get; set; }
          public string street2 { get; set; }
          public string city { get; set; }
          public string state { get; set; }
          public string postal_code { get; set; }
          public string s_status { get; set; }
          public string system_state { get; set; }
      }
      

      ...但如果 JSON 格式正确,这样 Locations 实际上是一个位置对象数组,那会更好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-21
        • 1970-01-01
        相关资源
        最近更新 更多