我试图让下面的JSON(见下文)进行反序列化(使用newtonsoft JSON序列化),问题是名为2010-12这显然是依赖于返回数据的变量 - 它重新presents一个月,下个月的值将变为2010。
这是我怎么可以和下面的类处理这个任何想法?
[JSONObject的(MemberSerialization.OptIn)
公共类犯罪
{
[JsonProperty()]
公共逐月{获得;组; }
}
例如JSON实例:
{
解说:空,
罪行:{
2010-12:{
反社会行为: {
crime_rate:0.46,
crime_level:平均,
total_crimes:74
},
其他犯罪:{
crime_rate:0.35,
crime_level:平均,
total_crimes:56
},
所有犯罪:{
crime_rate:1.12,
crime_level:平均,
total_crimes:180
},
抢劫: {
crime_rate:0.02,
crime_level:above_average,
total_crimes:3
},
窃:{
crime_rate:0.14,
crime_level:above_average,
total_crimes:22
},
车辆罪:{
crime_rate:0.04,
crime_level:平均,
total_crimes:7
},
暴力犯罪: {
crime_rate:0.11,
crime_level:平均,
total_crimes:18
}
}
}
}
解决方案
我不熟悉Newtonsoft的解串器,但总的来讲,我认为我们该做的是将反序列化的犯罪属性作为字典,字符串或日期时间的关键。对不起,我不能告诉你究竟是如何做到这一点的Newtonsoft,而是试图寻找到它。
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
}
}
}
}
解决方案
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.