3

I am getting a response back from a RESTful API in the form of a JSON object. Normally, I can parse this just fine when the keys are known. For instance, I create a User class like this:

[DataContract]
public class User
{
    [DataMember]
    public string id { get; set; }

    [DataMember]
    public string name { get; set; }

    [DataMember]
    public string email { get; set; }
}

All I have to do is pull read the response into my JSON deserializer and tell it the output is a <User> and I'm good to go. This falls short when making other requests, such as sales data. The response I get back is something like this:

{
  "2010-11-24": {
    "country": null,
    "iso": null,
    "product": null,
    "downloads": 39,
    "net_downloads": 38,
    "updates": 6,
    "revenue": "19.02",
    "returns": 1,
    "gift_redemptions": 0,
    "promos": 0
  },
  "2010-11-25": {
    "country": null,
    "iso": null,
    "product": null,
    "downloads": 63,
    "net_downloads": 63,
    "updates": 6,
    "revenue": "37.00",
    "returns": 0,
    "gift_redemptions": 0,
    "promos": 0
  }
}

If I could model this class as a [DataContract] then I'd be golden, but since the first key is a date, I can't hard code that.

Is there a JSON library out there that can take this kind of response and turn it into a strongly-typed C# class?

For the record, I'm using the JSONHelper used in this SO question.

Community
  • 1
  • 1
Nathan DeWitt
  • 6,511
  • 8
  • 46
  • 66

1 Answers1

5

JSON.NET is recognized as the best/fastest/most stable JSON C# library. I've used it, it works as advertised.

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
  • So, I've downloaded and looked through the documentation. It looks like I can do exactly what I've done before, create a class to deserialize my JSON into... but I'm still stuck. How do I deserialize into a class where I don't know the key? – Nathan DeWitt Jan 11 '12 at 17:06