Deserialize JSON with dynamic objects
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:
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; }}