开发者

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

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,
    "开发者_Python百科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
            }
        }
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜