从JSON在那里可以单T类型的对象或T的数组列表&LT反序列; T&GT; [英] Deserialize from json where can be single T object or array of T into List<T>
问题描述
我有code是这样的:
VAR JSON = GetJsonData(路径);
JObject event_dates_data = JObject.Parse(JSON);
VAR event_dates_list = JObject.Parse(event_dates_data [文件] [日]的ToString());
VAR event_dates = JsonConvert.DeserializeObject&LT;列表&LT; EVENTDATE&GT;&GT;(event_dates_list.ToString());
的Json可包含数组的物体(例如日期:[{},{},{}])或仅一个(例如日期:{})
JSON的样子了:
{
文件: {
结果:成功,
因此code:000000,
注意:空,
totaldates:1,
日期: {
DATE_ID:351314,
活:n的,
datestart:2012-03-07,
dateend:2015年3月7日,
timestart:12:00,
timeend:14点00,
date_available:10000
}
}
}
或者
{
文件: {
结果:成功,
因此code:000000,
注意:空,
totaldates:4,
日期: [
{
DATE_ID:346022,
活:n的,
datestart:2011-02-19,
dateend:2011-02-19,
timestart:12:00,
timeend:14点00,
date_available:10000
},
{
DATE_ID:346023,
活:n的,
datestart:2011年2月20日,
dateend:2011年2月20日,
timestart:12:00,
timeend:14点00,
date_available:10000
},
{
DATE_ID:346024,
活:n的,
datestart:2011-02-21,
dateend:2011-02-21,
timestart:12:00,
timeend:14点00,
date_available:10000
},
{
DATE_ID:546580,
活:Y,
datestart:2015年8月15日,
dateend:2015年8月15日,
timestart:12:00,
timeend:14点00,
date_available:10000
}
]
}
}
我有POCO的日期:
公共类EVENTDATE {
[JsonProperty(DATE_ID)]
公共字符串ID {获得;组; }
[JsonProperty(活)
[JsonConverter(typeof运算(AvailableForSalesFiledConverter))]
公共BOOL AvailableForSales {获得;组; }
[JsonProperty(datestart)]
公共字符串DateStart {获得;组; }
[JsonProperty(dateend)]
公共字符串DateEnd {获得;组; }
[JsonProperty(timestart)]
公共字符串TimeStart {获得;组; }
[JsonProperty(timeend)]
公共字符串TimeEnd {获得;组; }
[JsonProperty(date_available)]
公众诠释DateAvailable {获得;组; }
}
所以,当我试图反序列化我得到异常: 无法反序列化的当前JSON对象(例如{\名称\:\价值\}) 入式'System.Collections.Generic.List`1 [TicketProvider.BrownPaperTickets.Entities.EventDate] 因为该类型需要一个JSON阵列(例如[1,2,3]),以正确的反序列化。\ r \ n要 解决这个错误要么改变的JSON到一个JSON阵列(例如[1,2,3]) 或改变反序列化类型,以便它是一个正常的.NET类型 (例如,不是一个原始类型类似整数,而不是像数组或列表的集合型) 可以从一个JSON对象反序列化。 JsonObjectAttribute也可以加入到 键入迫使它从一个JSON对象反序列化。\ r \ n路径DATE_ID',2号线,13位 我怎样才能得到它列出?
VAR JSON = GetJsonData(路径);
JObject event_dates_data = JObject.Parse(JSON);
VAR event_dates_list = JObject.Parse(event_dates_data [文件] [日]的ToString());
event_dates_list =的String.Format([{0}],event_dates_list.Trim('[',']'));
VAR event_dates = JsonConvert.DeserializeObject&LT;列表&LT; EVENTDATE&GT;&GT;(event_dates_list.ToString());
I have the code like this:
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 may contains an array objects (for example "date:[{},{},{}]") or only one (for example "date:{}")
Json looks like that:
{
"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"
}
}
}
Or:
{
"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"
}
]
}
}
I have the poco for the "date":
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; }
}
So when I'm trying to deserialize I getting exception: "Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[TicketProvider.BrownPaperTickets.Entities.EventDate]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'date_id', line 2, position 13." How can I get it to List?
var json = GetJsonData(path);
JObject event_dates_data = JObject.Parse(json);
var event_dates_list = JObject.Parse(event_dates_data["document"]["date"].ToString());
event_dates_list = string.Format("[{0}]", event_dates_list.Trim('[', ']'));
var event_dates = JsonConvert.DeserializeObject<List<EventDate>>(event_dates_list.ToString());
这篇关于从JSON在那里可以单T类型的对象或T的数组列表&LT反序列; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!