4

I'm trying to get the following json (see below) deserialized (using newtonsoft json serializer) and the problem is the variable named "2010-12" it is obviously dependent on the data returned - it represents a month and next month the value will change to "2010-01".

Any ideas on how i could handle this with the following class?

[JsonObject(MemberSerialization.OptIn)]
public class Crimes
{
   [JsonProperty()]
   public Month Month { get; set; }
}

Example JSON instance:

{
    "commentary": null,
    "crimes": {
        "2010-12": {
            "anti-social-behaviour": {
                "crime_rate": "0.46",
                "crime_level": "average",
                "total_crimes": 74
            },
            "other-crime": {
                "crime_rate": "0.35",
                "crime_level": "average",
                "total_crimes": 56
            },
            "all-crime": {
                "crime_rate": "1.12",
                "crime_level": "average",
                "total_crimes": 180
            },
            "robbery": {
                "crime_rate": "0.02",
                "crime_level": "above_average",
                "total_crimes": 3
            },
            "burglary": {
                "crime_rate": "0.14",
                "crime_level": "above_average",
                "total_crimes": 22
            },
            "vehicle-crime": {
                "crime_rate": "0.04",
                "crime_level": "average",
                "total_crimes": 7
            },
            "violent-crime": {
                "crime_rate": "0.11",
                "crime_level": "average",
                "total_crimes": 18
            }
        }
    }
}
AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152

1 Answers1

2

I'm not familiar with Newtonsoft's deserializer, but generally speaking I think the thing to do would be to deserialize the crimes property as a dictionary with either a string or DateTime key. Sorry I can't tell you exactly how to do that in Newtonsoft, but try looking into it.

Misko
  • 2,044
  • 12
  • 15