2016-12-01 27 views
0

我有以下JSON。它可以使用下面的代碼反序列化。如何在C#中反序列化複雜的字典列表JSON?

List<Dictionary<string, Dictionary<string, Dictionary<string, int>>>> listOfOptions = JsonConvert.DeserializeObject<List<Dictionary<string, Dictionary<string, Dictionary<string, int>>>>>(JSONdata); 

我想按照類結構來構造listOfOptions


的fieldName = 「prioritycode」

fieldOption 1 =>密鑰=高,值= 1

fieldOption 2 =>密鑰=正常,值= 2

fieldOption 3 => Key = Low,Value = 3

isescalatedfirstresponsesent的相同方式。

internal class DropOptions 
{ 
    public string fieldName { get; set; } 
    public Dictionary<string,int> fieldOptions { get; set; } 

} 

我已經試過不夠好,但不能構建:(

任何人可以幫我

這是我的JSON

[{ 「prioritycode」:{ 「高」:{ 「高」:1}, 「普通」:{ 「普通」:2}, 「低」:{ 「低」:3}}},{ 「isescalated」:{ 「是」:{」是 「:1},」 否 「:{」 否 「:0}}},{」 firstresponsesent 「:{」 是 「:{」 是 「:1},」 否 「:{」 否「:0}} }]

[ 
    { 
     "prioritycode": 
     { 
      "High": 
      { 
       "High":1 
      }, 
      "Normal": 
      { 
       "Normal":2 
      }, 
      "Low": 
      { 
       "Low":3 
      } 
     } 
    }, 
    { 
     "isescalated": 
     { 
      "Yes": 
      { 
       "Yes":1 
      }, 
      "No": 
      { 
       "No":0 
      } 
     } 
    }, 
    { 
     "firstresponsesent": 
     { 
      "Yes": 
      { 
       "Yes":1 
      }, 
      "No": 
      { 
       "No":0 
      } 
     } 
    } 
] 
+1

老實說,只是使用類映射。而不是一本藏有詞典關鍵字的詞典集合http://json2csharp.com/ – ColinM

+0

@ json2csharp.com你可以給我點子嗎?我已經浪費了足夠的時間。 –

回答

0

您可以使用Json.Net,只是喜歡寫東西var obj = JsonConvert.DeserializeObject<dynamic>(json);
反序列化你提供我能寫int normal = obj[0].prioritycode.Normal.Normal;normal可變正拿着2 JSON之後。不需要額外的課程。

-1

這使用Newtonsoft.Json(Json.NET)框架將JSON反序列化爲RootObject的集合。使用強類型類的好處在於您有編譯時檢查,以及您不試圖訪問不存在的屬性或拼寫錯誤的屬性。

public class RootObjectDeserializer 
{ 
    List<RootObject> DeserializeRoot() 
    { 
     // Read string from somewhere 
     string json = "[{\"prioritycode\":{\"High\":{\"High\":1},\"Normal\":{\"Normal\":2},\"Low\":{\"Low\":3}}},{\"isescalated\":{\"Yes\":{\"Yes\":1},\"No\":{\"No\":0}}},{\"firstresponsesent\":{\"Yes\":{\"Yes\":1},\"No\":{\"No\":0}}}]"; 
     // Deserialize to a List of RootObject 
     List<RootObject> deserializedObjects = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RootObject>>(json); 
     // Return RootObject 
     return deserializedObjects; 
    } 
} 

//Class mappings below 
public class High 
{ 
    [JsonProperty("High")] 
    public int HighProperty { get; set; } 
} 

public class Normal 
{ 
    [JsonProperty("Normal")] 
    public int NormalProperty { get; set; } 
} 

public class Low 
{ 
    [JsonProperty("Low")] 
    public int LowProperty { get; set; } 
} 

public class Prioritycode 
{ 
    public High High { get; set; } 
    public Normal Normal { get; set; } 
    public Low Low { get; set; } 
} 

public class Yes 
{ 
    [JsonProperty("Yes")] 
    public int YesProperty { get; set; } 
} 

public class No 
{ 
    [JsonProperty("No")] 
    public int NoProperty { get; set; } 
} 

public class Isescalated 
{ 
    public Yes Yes { get; set; } 
    public No No { get; set; } 
} 

public class Yes2 
{ 
    public int Yes { get; set; } 
} 

public class No2 
{ 
    public int No { get; set; } 
} 

public class Firstresponsesent 
{ 
    public Yes2 Yes { get; set; } 
    public No2 No { get; set; } 
} 

public class RootObject 
{ 
    public Prioritycode prioritycode { get; set; } 
    public Isescalated isescalated { get; set; } 
    public Firstresponsesent firstresponsesent { get; set; } 
} 
+0

如果字段名稱已修復,您的方式將會起作用。您已修復字段的名稱。就我而言,它可以是任何東西。我們不能依賴修復字段(prioritycode,isescalated,firstresponsesent)或修復選項(高,正常,低)。 –

+0

爲什麼你投下有效答案,因爲他們不符合你未提及的要求中的特定標準?這很簡單,只是不要將它們標記爲答案。 – ColinM

0

我已經完成了這個。它看起來很好。我知道這有點困難並且有循環。但是選項(prioritycode,isescalated,firstresponsesent)是動態的。它可以更多或更少。它的名稱可以是任何其他類似taskStatus,productType,productCategory等

List<DropOptions> listOfOptions = new List<DropOptions>(); 
foreach (Dictionary<string, Dictionary<string, Dictionary<string, int>>> item in tempListOfOptions) 
{ 
    foreach (Dictionary<string, Dictionary<string, int>> item1 in item.Values) 
    { 
     DropOptions objDrop = new DropOptions(); 
     objDrop.fieldName = item.Select(t => t.Key).FirstOrDefault(); 
     objDrop.fieldOptions = new Dictionary<string, int>(); 
     foreach (Dictionary<string, int> item2 in item1.Values) 
     { 
      string strKey = item2.Select(t => t.Key).FirstOrDefault(); 
      int strValue = item2.Select(t => t.Value).FirstOrDefault(); 

      objDrop.fieldOptions.Add(strKey, strValue); 
     } 
     listOfOptions.Add(objDrop); 
    } 
}