2

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

3 Answers3

3

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; }
}
Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
-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.

Ulric
  • 826
  • 5
  • 16
  • This is (except for the name of the `_98765` class) exactly the same as OPs class structure generated by json2csharp. The JSON is fine, but it is just one example from a JSON "schema" that we have to infer ourselves using some common sense (i.e. by noting that there could be multiple locations, and that the value '98765' is just an identifier value associated with one of them). – Alex Mar 31 '15 at 19:42
-2

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.