我正在处理一个不一致的Json数据,想要将其反序列化为我的.Net类。问题是有时我有一个对象,有时它是一个数组。如何最好地解决这个问题?使用转换器?以下是我的Json数据片段:
[
{
"@class": "odd",
"td": [
{
"@class": "user",
"@onmouseover": "userInfo('469');",
"@onmouseout": "userInfo(0);",
"@onmousemove": "moveSlotInfo();",
"#text": " AAA"
},
{
"@id": "day-469-2014-04-07",
"@style": "vertical-align: top;",
"table": {
"@class": "ss",
"@cellspacing": "1",
"tbody": {
"tr": {
"td": {
"@class": "as",
"@style": "color: #ffffff; background-color: #4040ff;",
"@onmouseover": "this.className=(document.week_vs_doctor.activityId.value==-1?'sd':'sp');slotInfo('177935',false);",
"@onmouseout": "this.className='as';slotInfo(0,false);",
"@onmousemove": "moveSlotInfo();",
"#text": "KAVAul"
}
}
}
}
}
]
},
{
"@class": "even",
"td": [
{
"@class": "user",
"@onmouseover": "userInfo('262');",
"@onmouseout": "userInfo(0);",
"@onmousemove": "moveSlotInfo();",
"#text": " BBB"
},
{
"@id": "day-262-2014-04-07",
"@style": "vertical-align: top;",
"table": {
"@class": "ss",
"@cellspacing": "1",
"tbody": {
"tr": [
{
"td": {
"@class": "as",
"@style": "color: #ffffff; background-color: #4040ff;",
"@onmouseover": "this.className=(document.week_vs_doctor.activityId.value==-1?'sd':'sp');slotInfo('174318',false);",
"@onmouseout": "this.className='as';slotInfo(0,false);",
"@onmousemove": "moveSlotInfo();",
"#text": "KAVA "
}
},
{
"td": {
"@class": "as",
"@style": "color: #000000; background-color: #ffc0c0;",
"@onmouseover": "this.className=(document.week_vs_doctor.activityId.value==-1?'sd':'sp');slotInfo('174338',false);",
"@onmouseout": "this.className='as';slotInfo(0,false);",
"@onmousemove": "moveSlotInfo();",
"#text": "Dagbak"
}
}
]
}
}
}
]
}
]
问题出在 tr 对象上,它可能是对象(Object)或数组(Array)。
下面是我的代码片段:
public class Td2
{
[JsonProperty("@class")]
public string TdClass { get; set; }
[JsonProperty("@style")]
public string style { get; set; }
[JsonProperty("@onmouseover")]
public string onmouseover { get; set; }
[JsonProperty("@onmouseout")]
public string onmouseout { get; set; }
[JsonProperty("@onmousemove")]
public string onmousemove { get; set; }
[JsonProperty("#text")]
public string text { get; set; }
}
public class Tr2
{
public Td2 td { get; set; }
}
public class Tbody2
{
[JsonProperty]
[JsonConverter(typeof(ScheduleJsonConverter<Tr2>))]
public List<Tr2> tr { get; set; }
}
public class Table
{
[JsonProperty("@cellspacing")]
public string cellspacing { get; set; }
public Tbody2 tbody { get; set; }
[JsonProperty("@class")]
public string tClass { get; set; }
}