C# 将复杂对象列表反序列化为类型的精确列表
有没有办法将复杂对象列表反序列化为已知类型的列表: 例: 这是我想反序列化到的对象 我就是这样尝试的 : 内部异常:C# 将复杂对象列表反序列化为类型的精确列表,c#,json-deserialization,C#,Json Deserialization,有没有办法将复杂对象列表反序列化为已知类型的列表: 例: 这是我想反序列化到的对象 我就是这样尝试的 : 内部异常: "InnerException": { "Message": "An error has occurred.", "ExceptionMessage": "Could not cast or convert from System.Int64 to System.Collections.Generic.IList`1[Ubisoft.ECM.Events.Busi
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "Could not cast or convert from System.Int64 to System.Collections.Generic.IList`1[Ubisoft.ECM.Events.Business.Providers.EmployeeProvider.Employee].",
"ExceptionType": "System.ArgumentException",
"StackTrace": " at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)\r\n at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)"
}
谢谢。所以为了反序列化到列表,我必须引用System.Web.Script.Serialization 创建正确的对象
public class MyType
{
public string Prop1Name{ get; set; }
public string Prop2Name{ get; set; }
public string PropNName{ get; set; }
}
并使用命名空间中的JavaScriptSerializer
List<MyType> x = new JavaScriptSerializer().Deserialize<List<MyType>>(_content);
List x=new JavaScriptSerializer()。反序列化(_内容);
谢谢您检查过这个了吗?是的,很好,谢谢
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "Could not cast or convert from System.Int64 to System.Collections.Generic.IList`1[Ubisoft.ECM.Events.Business.Providers.EmployeeProvider.Employee].",
"ExceptionType": "System.ArgumentException",
"StackTrace": " at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)\r\n at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)"
}
public class MyType
{
public string Prop1Name{ get; set; }
public string Prop2Name{ get; set; }
public string PropNName{ get; set; }
}
List<MyType> x = new JavaScriptSerializer().Deserialize<List<MyType>>(_content);