我序列化下面的类Newtonsoft。Json,但我不能反序列化相同的Json Newtonsoft.Json。我怎么能做到呢?
Json:"{"UserEvents":[{"id":1214308,"Date":20150801000000,"IsRead":true}]}"
我的实体:
public class UserEventLog {
[JsonProperty("UserEvents")]
public List<UserEvent> UserEvents { get; set; }
public UserEventLog() {
UserEvents = new List<UserEvent>();
}
}
public class UserEvent {
[JsonProperty("id")]
public long id{ get; set; }
[JsonProperty("Date")]
public long Date{ get; set; }
[JsonProperty("IsRead")]
public bool IsRead { get; set; }
}
我的反序列化器是:
List<UserEventLog> convert = JsonConvert.DeserializeObject<List<UserEventLog>>(user.ToString()) as List<UserEventLog>;
但是产生错误:
类型为'Newtonsoft.Json '的未处理异常。JsonSerializationException'在Newtonsoft.Json.dll中发生
附加信息:将值
"{"UserEvents":[{"id":1214308,"Date":20150801000000,"IsRead":true}]}"
转换为类型'System.Collections.Generic.List ' 1出错
我怎么解决它?我怎么能反序列化我的对象列表到UserEvents列表?
这适用于linqpad:
void Main()
{
var user = "{"UserEvents":[{"id":1214308,"Date":20150801000000,"IsRead":true}]}";
UserEventLog convert = JsonConvert.DeserializeObject<UserEventLog>(user.ToString());
convert.UserEvents.Count().Dump();
}
public class UserEventLog
{
[JsonProperty("UserEvents")]
public List<UserEvent> UserEvents { get; set; }
public UserEventLog()
{
UserEvents = new List<UserEvent>();
}
}
public class UserEvent
{
[JsonProperty("id")]
public long id { get; set; }
[JsonProperty("Date")]
public long Date { get; set; }
[JsonProperty("IsRead")]
public bool IsRead { get; set; }
}
问题是,你试图反序列化到列表,但它不是一个数组的UserEvents