【发布时间】:2014-10-24 04:44:36
【问题描述】:
我有这样的代码:
var json = GetJsonData(path);
JObject event_dates_data = JObject.Parse(json);
var event_dates_list = JObject.Parse(event_dates_data["document"]["date"].ToString());
var event_dates = JsonConvert.DeserializeObject<List<EventDate>>(event_dates_list.ToString());
Json 可能包含一个数组对象(例如“date:[{},{},{}]”)或只包含一个(例如“date:{}”)
Json 看起来像这样:
{
"document": {
"result": "success",
"resultcode": "000000",
"note": null,
"totaldates": "1",
"date": {
"date_id": "351314",
"live": "n",
"datestart": "2012-03-07",
"dateend": "2015-03-07",
"timestart": "12:00",
"timeend": "14:00",
"date_available": "10000"
}
}
}
或者:
{
"document": {
"result": "success",
"resultcode": "000000",
"note": null,
"totaldates": "4",
"date": [
{
"date_id": "346022",
"live": "n",
"datestart": "2011-02-19",
"dateend": "2011-02-19",
"timestart": "12:00",
"timeend": "14:00",
"date_available": "10000"
},
{
"date_id": "346023",
"live": "n",
"datestart": "2011-02-20",
"dateend": "2011-02-20",
"timestart": "12:00",
"timeend": "14:00",
"date_available": "10000"
},
{
"date_id": "346024",
"live": "n",
"datestart": "2011-02-21",
"dateend": "2011-02-21",
"timestart": "12:00",
"timeend": "14:00",
"date_available": "10000"
},
{
"date_id": "546580",
"live": "y",
"datestart": "2015-08-15",
"dateend": "2015-08-15",
"timestart": "12:00",
"timeend": "14:00",
"date_available": "10000"
}
]
}
}
我有“日期”的 poco:
public class EventDate {
[JsonProperty("date_id")]
public string Id { get; set; }
[JsonProperty("live")]
[JsonConverter(typeof(AvailableForSalesFiledConverter))]
public bool AvailableForSales { get; set; }
[JsonProperty("datestart")]
public string DateStart { get; set; }
[JsonProperty("dateend")]
public string DateEnd { get; set; }
[JsonProperty("timestart")]
public string TimeStart { get; set; }
[JsonProperty("timeend")]
public string TimeEnd { get; set; }
[JsonProperty("date_available")]
public int DateAvailable { get; set; }
}
所以当我尝试反序列化时,我得到了异常: "无法反序列化当前 JSON 对象(例如 {\"name\":\"value\"}) 进入类型'System.Collections.Generic.List`1 [TicketProvider.BrownPaperTickets.Entities.EventDate]' 因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。\r\nTo 修复此错误或者将 JSON 更改为 JSON 数组(例如 [1,2,3]) 或更改反序列化类型,使其成为普通的 .NET 类型 (例如,不是像整数这样的原始类型,也不是像数组或列表这样的集合类型) 可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到 类型以强制它从 JSON 对象反序列化。\r\n路径 'date_id',第 2 行,第 13 位。" 我怎样才能把它列入清单?
【问题讨论】: