11

I have a JSON object that comes with a long list of area codes. Unfortunately each area code is the object name on a list in the Data object. How do I create a class that will allow RestSharp to deserialize the content?

Here's how my class looks now:

public class phaxioResponse
{
    public string success { get; set; }
    public string message { get; set; }
    public List<areaCode> data { get; set; }

    public class areaCode
    {
        public string city { get; set; }
        public string state { get; set; }
    }
}

And here's the JSON content:

{
    success: true
    message: "277 area codes available."
    data: {
        201: {
            city: "Bayonne, Jersey City, Union City"
            state: "New Jersey"
        }
        202: {
            city: "Washington"
        state: "District Of Columbia"
        } [...]
}
Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
Peter Schrøder
  • 494
  • 1
  • 4
  • 22
  • 2
    That json is all sorts of fouled up. – Pete Garafano May 06 '14 at 21:49
  • And do you really expect anyone to read all of that? – John Saunders May 06 '14 at 21:50
  • 1
    @JohnSaunders you don't need to read the entire json object through - I'm pretty sure you can see that there's only three nodes in the main object and the data object is the same all the way through? – Peter Schrøder May 06 '14 at 21:51
  • 1
    If we don't need to read it, then why post it? – John Saunders May 06 '14 at 21:52
  • Can we ask where this "Json" is coming from? We may be able to help a little bit more, since it is clearly not valid JSON. – Pete Garafano May 06 '14 at 22:10
  • @PeteGarafano yes definitely... You can make a post request to this url: https://api.phaxio.com/v1/areaCodes – Peter Schrøder May 07 '14 at 03:04
  • 2
    I don't understand the scorn directed at the asker here, people will call basically any old trash json and expect you to handle it. I've seen "json" data which is a list of lists where the first list is the column names, and the remainder are the lists of values... – Toadfish Oct 20 '16 at 04:08

4 Answers4

18

Since this JSON is not C# friendly, I had to do a little bit of hackery to make it come out properly. However, the result is quite nice.

var json = JsonConvert.DeserializeObject<dynamic>(sampleJson);
var data = ((JObject)json.data).Children();
var stuff = data.Select(x => new { AreaCode = x.Path.Split('.')[1], City = x.First()["city"], State = x.Last()["state"] });

This code will generate an anonymous type that best represents the data. However, the anonymous type could be easily replaced by a ctor for a more normal DTO class.

The output looks something like this:

Deserialization Output

Pete Garafano
  • 4,863
  • 1
  • 23
  • 40
2

your json is incorrect, but if you do correct it you can use a json-to-csharp tool like the one on http://json2csharp.com/ to generate your classes:

public class __invalid_type__201
{
    public string city { get; set; }
    public string state { get; set; }
}

public class Data
{
    public __invalid_type__201 __invalid_name__201 { get; set; }
}

public class RootObject
{
    public bool success { get; set; }
    public string message { get; set; }
    public Data data { get; set; }
}
Z .
  • 12,657
  • 1
  • 31
  • 56
  • 1
    That's a great idea... Unfortunately the JSON is not in my hands - it comes from an external API – Peter Schrøder May 06 '14 at 21:56
  • 1
    @PeteFox it's not valid JSON. All the field names should be in quotes to be considered valid JSON. That is why json2csharp.com is generating invalid classes. – Pete Garafano May 06 '14 at 22:09
1

I don't know anything about RestSharp, but if you're using Newtonsoft on the server side, then you can just pass a JObject to your method. Then you can interrogate the object to see what type of object it really is and use JObject.ToObject() to convert it.

Ash8087
  • 643
  • 6
  • 16
0

I think using Dictionary<int,areaCode> is the easiest way.

public class phaxioResponse
    {
        public string success { get; set; }
        public string message { get; set; }
        public Dictionary<int,areaCode> data { get; set; }

        public class areaCode
        {
            public string city { get; set; }
            public string state { get; set; }
        }
    }

Then:

    var res= JsonConvert.DeserializeObject<phaxioResponse>(json);
    Console.WriteLine(string.Join(",", res.data));
MistyK
  • 6,055
  • 2
  • 42
  • 76