2013-01-01 34 views
0

新年快樂:-)C#4反序列化JSON複雜的對象列表<int>

我有以下的JSON對象:

{ 
\"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 
        } 

       } 
      } 
     } 
    } 

該模型的定義是:

public class DeliveryDetailsView 
{ 
    public int OutcomeSummaryID { get; set; } 
    public int DeliveryDetailsID { get; set; } 
    public List<int> AttemptedDeliveryModesIDList { get; set; } 
} 

當我運行,這是出現以下情況例外:

System.InvalidCastException was caught 
Message=Object must implement IConvertible. 

這是一個大項目的提取物和我已經使用這個方法廣泛的其他地方,但是這是第一次用一個列表作爲一個字段和我可以看不到乾淨的方式來解決這個問題。我可以使用

if (localType.IsCollectionType()) 

但是不知道從哪裏開始(Google的答案似乎都不符合這種情況,大部分都與XML有關)。

在此先感謝。

UPDATE

感謝@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); 
    } 
} 

...但是,所有嘗試將JSON數組轉換爲List都失敗。我也試圖

List<int> tempList = (List<int>)dataModel[propInfo.Name]; 

兩個拋出異常

Unable to cast object of type 'System.Object[]' ... 

任何進一步的想法

感謝

回答

1

我前段時間有類似的問題。我不得不用特殊的方式處理非原始類型,如下所示:

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); 
} 
+0

感謝您的啓動;我真的(真的)需要的是如何處理JSON的extracint到我的模型的字段 –

+0

dataModel [propInfo.Name]應該是可枚舉的東西。我認爲你必須重複它並建立一個List 。 Probaly與新列表(dataModel [propInfo.Name])一樣簡單。這個列表應該適用於'propInfo.SetValue'。 'ChangeType'不會爲你完成這項工作。 – CubeSchrauber

+0

現在無法創建列表 - 請參閱上面的更新。再次感謝所有幫助。 –

0

您可以用下面的代碼

public List<int> AttemptedDeliveryModesIDList { get; set; } 

機智嘗試h

public int[] AttemptedDeliveryModesIDList { get; set; } 

我還沒有試過這個代碼。所以我不是100%肯定的

+0

感謝這個;我希望得到一些關於如何使用列表的東西。如果一切都失敗了,我一定會試試這個。 –