How to serialize\deserialize a property where the property name is dependent on the data

Feb 22, 2011 at 2:51 PM

I'm trying to get the following json (see below) deserialized 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
    {
        #region Properties

        [JsonProperty()]
        public Month Month { get; set; }

        #endregion
    }

 

 

{
"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
}
}
}
}

 

 

Feb 24, 2011 at 12:44 PM

Post the same question on SO and got the answer to use a dictionary - which is obvious when I think about it :)

 

http://stackoverflow.com/questions/5079825/how-to-serialize-deserialize-a-property-where-the-property-name-is-dependent-on-t