Deserializing this JSON response to C#

[亡魂溺海] 提交于 2019-12-11 02:13:37

问题


I have this specific JSON response that I am trying to deserialize without success. I am hoping someone can help me.

Here is the JSON response I get:

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

}

I used json2csharp http://json2csharp.com and got these recommended classes:

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

But when I try to use it in my code:

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

What I get is (Watch):

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

Obviously I am not creating (json2csharp) the right classes for the DeserializeObject, and sadly I have no control over the JSON response (vendor = SimpliSafe).

It is obvious the "98765" is meant to be a value (location number) but json2csharp makes it into this __invalid_type__98765 class and this is probably why it gets null.

Any idea how should the classes look for this particular JSON to be successfully deserialized?

Thanks! Zachs


回答1:


You should be able to do this with a dictionary:

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:


Do you have a non-Express version of Visual Studio? If so, copy the JSON to clipboard and then go to the Visual Studio menu: Edit >> Paste special >> Paste JSON as classes.

Using that gives:

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

That suggests your JSON structure is not quite right.




回答3:


You can also specify the property name via an attribute to use to get around this:

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

...but it would really be better if the JSON were properly formatted such that the Locations was actually an array of location objects.



来源:https://stackoverflow.com/questions/29376491/deserializing-this-json-response-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!