List c#4反序列化JSON复杂对象列表<;int>;
新年快乐:-) 我有以下JSON对象:List c#4反序列化JSON复杂对象列表<;int>;,list,c#-4.0,serialization,List,C# 4.0,Serialization,新年快乐:-) 我有以下JSON对象: { \"OutcomeSummaryID\":105, \"DeliveryDetailsID\":9, \"AttemptedDeliveryModesIDList\":[1,5], } 我正在使用以下方法对其进行反序列化: private void SerializeModel<T>(IDictionary<string, object> dataModel, T myModel) { Type sou
{
\"OutcomeSummaryID\":105,
\"DeliveryDetailsID\":9,
\"AttemptedDeliveryModesIDList\":[1,5],
}
我正在使用以下方法对其进行反序列化:
private void SerializeModel<T>(IDictionary<string, object> dataModel, T myModel)
{
Type sourceType = typeof(T);
foreach (PropertyInfo propInfo in (sourceType.GetProperties()))
{
if (dataModel.ContainsKey(propInfo.Name))
{
// if an empty string has been returned don't change the value
if (dataModel[propInfo.Name].ToNullSafeString() != String.Empty)
{
try
{
Type localType = propInfo.PropertyType;
localType = Nullable.GetUnderlyingType(localType) ?? localType;
propInfo.SetValue(myModel, Convert.ChangeType(dataModel[propInfo.Name], localType), null);
}
catch (Exception e)
{
// ToDo: log serialize value errors
}
}
}
}
}
这是从一个更大的项目中提取的,我在其他地方广泛使用了这种方法,但这是第一次将列表作为字段,我看不到解决这个问题的干净方法。我可以用
if (localType.IsCollectionType())
但我不知道接下来该怎么做(谷歌的答案似乎都不符合这种情况,而且大部分都与XML有关)
提前谢谢
更新
多亏了下面的@Cube,我现在得到了部分答案
if (localType.IsGenericType && localType.GetGenericTypeDefinition().Equals(typeof(List<>)))
{
Type localListType = localType.GetGenericArguments()[0];
if (localListType.Equals(typeof(int)))
{
IDictionary<string, object> dataItem = (Dictionary<string, object>)dataModel[propInfo.Name];
List<int> tempList = new List<int>();
foreach (var item in dataItem)
{
tempList.Add((int)item.Value);
}
propInfo.SetValue(perinatalModel, tempList, null);
}
}
还有什么想法吗
谢谢您可以尝试使用以下代码
public List<int> AttemptedDeliveryModesIDList { get; set; }
我还没有试过这个密码。所以我不是100%确定不久前我也有类似的问题。我必须以特殊的方式处理非基本类型,如下所示:
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(List<>)))
{
Type u = t.GetGenericArguments()[0];
if (u.Equals(typeof(int)))
// now dealing with List<int>
...
}
if(t.IsGenericType&&t.GetGenericTypeDefinition().Equals(typeof(List)))
{
类型u=t.GetGenericArguments()[0];
if(u.Equals(typeof(int)))
//现在处理列表
...
}
希望这个简短的片段能有所帮助
更新更新:
if (localListType.Equals(typeof(int)))
{
object[] dataItem = (object[])dataModel[propInfo.Name];
List<int>tempList = new List<int>();
foreach(object probablyInteger in dataItem)
tempList.Add(Convert.ToInt32(probablyInteger));
propInfo.SetValue(perinatalModel, tempList, null);
}
if(localListType.Equals(typeof(int)))
{
对象[]数据项=(对象[])数据模型[propInfo.Name];
ListtempList=新列表();
foreach(数据项中的对象可能是整数)
添加(Convert.ToInt32(可能是整数));
SetValue(围产期模型,模板,null);
}
谢谢你;我希望得到一些关于如何使用列表的信息。如果其他一切都失败了,那么我一定会试试这个;我真正(真正)需要的是如何处理将JSON提取到我的modeldataModel的列表字段中[propInfo.Name]应该是可枚举的。我认为你必须反复阅读它并从中创建一个列表。很可能像新列表一样简单(dataModel[propInfo.Name])
。此列表应适用于propInfo.SetValue
<代码>更改类型无法为您执行此操作。现在无法创建列表-请参阅上面的更新。再次感谢您的帮助。因为我不知道dataModel[propInfo.Name]
实际上是什么,所以我不能说如何转换它。由于错误消息,我将尝试object[]templast=(object[])数据模型[propInfo.Name]
。这个对象本身很有希望是或可以转换成整数。最后的建议完美地完成了这个解决方案-非常感谢
Unable to cast object of type 'System.Object[]' ...
public List<int> AttemptedDeliveryModesIDList { get; set; }
public int[] AttemptedDeliveryModesIDList { get; set; }
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(List<>)))
{
Type u = t.GetGenericArguments()[0];
if (u.Equals(typeof(int)))
// now dealing with List<int>
...
}
if (localListType.Equals(typeof(int)))
{
object[] dataItem = (object[])dataModel[propInfo.Name];
List<int>tempList = new List<int>();
foreach(object probablyInteger in dataItem)
tempList.Add(Convert.ToInt32(probablyInteger));
propInfo.SetValue(perinatalModel, tempList, null);
}