2015-03-31 35 views
2

我有這個特定的JSON響應,我試圖反序列化沒有成功。我希望有人能幫助我。反序列化這個JSON響應到C#

這裏是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 

顯然我不是創造(json2csharp)右側類的DeserializeObject,而黯然我有過JSON響應(供應商= SimpliSafe)沒有控制權。

很明顯,「98765」是一個值(位置編號),但json2csharp使它成爲__invalid_type__98765類,這可能是爲什麼它變爲空。

任何想法應該如何尋找這個特定的JSON被成功反序列化?

謝謝! Zachs

回答

3

你應該能夠用字典來做到這一點:

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

非常感謝! – 2015-03-31 19:39:16

-2

你有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的結構是不完全正確。

+0

這是(_98765類的名稱除外)與json2csharp生成的OPs類結構完全相同。 JSON很好,但它只是JSON「模式」的一個例子,我們必須使用一些常識來推斷自己(例如,注意到可能有多個位置,並且值'98765'只是一個標識符值與其中一個相關聯)。 – Alex 2015-03-31 19:42:20

+0

我不確定你想要做什麼,但是nvm。 – Ulric 2015-03-31 19:59:56

-2

您還可以通過屬性指定屬性名使用來解決這個問題:

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是正確的格式,使得位置實際上是一個位置對象數組。