簡體   English   中英

反序列化此JSON響應到C#

[英]Deserializing this JSON response to C#

我有這個特定的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響應(供應商= SimpliSafe)。

顯而易見,“ 98765”是一個值(位置編號),但是json2csharp使其成為__invalid_type__98765類,這可能就是為什么它為null的原因。

知道這些類如何查找要成功反序列化的特定JSON嗎?

謝謝! 扎克斯

您應該可以使用字典來做到這一點:

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; }
}

您是否有Visual Studio的非Express版本? 如果是這樣,請將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結構不太正確。

您也可以通過屬性指定屬性名稱,以解決此問題:

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正確地格式化為“位置”實際上是位置對象數組,那會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM