2017-05-12 70 views
-1

通用方法我有一個JSON對象,如下到平坦JSON數組轉換爲嵌套JSON

[ 
    { 
    "Id": 7, 
    "Name": "Colocation Folder", 
    "ParentId": 1, 
    "depth": 0 
    }, 
    { 
    "Id": 8, 
    "Name": "CoLo Real Estate", 
    "ParentId": 7, 
    "depth": 1 
    }, 
    { 
    "Id": 10, 
    "Name": "CoLo: Burst", 
    "ParentId": 7, 
    "depth": 1 
    }, 
    { 
    "Id": 34, 
    "Name": "CoLo Dedicated Bandwidth", 
    "ParentId": 7, 
    "depth": 1 
    }, 
    { 
    "Id": 10035, 
    "Name": "Infrastructure as a Service", 
    "ParentId": 7, 
    "depth": 1 
    }, 
    { 
    "Id": 10037, 
    "Name": "Software as a Service", 
    "ParentId": 7, 
    "depth": 1 
    }, 
    { 
    "Id": 10038, 
    "Name": "IaaS Component Upgrade", 
    "ParentId": 7, 
    "depth": 1 
    }, 
    { 
    "Id": 668, 
    "Name": "CoLo Misc Folder", 
    "ParentId": 7, 
    "depth": 1 
    }, 
    { 
    "Id": 758, 
    "Name": "CoLo: Conduit Fee", 
    "ParentId": 668, 
    "depth": 2 
    }, 
    { 
    "Id": 765, 
    "Name": "CoLo: Private VLAN", 
    "ParentId": 668, 
    "depth": 2 
    } 
] 

IdParentId字段顯示的項目之間的關係。我需要將它作爲使用C#的嵌套JSON。 由於會有很多這樣的模型,我不想爲每個模型創建單獨的類。在C#中是否有一個通用的方法,將採用一個扁平的JSON數組,將IDParentId字段作爲輸入,然後返回一個嵌套的JSON以及數組中的所有其他字段?例如,我要尋找的嵌套JSON的輸出如下:

[ 
    { 
    "Id": 7, 
    "Name": "Colocation Folder", 
    "items": [ 
     { 
     "Id": 8, 
     "Name": "CoLo Real Estate", 
     "ParentId": 7 
     }, 
     { 
     "Id": 10, 
     "Name": "CoLo: Burst", 
     "ParentId": 7 
     }, 
     { 
     "Id": 34, 
     "Name": "CoLo Dedicated Bandwidth", 
     "ParentId": 7 
     }, 
     { 
     "Id": 10035, 
     "Name": "Infrastructure as a Service", 
     "ParentId": 7 
     }, 
     { 
     "Id": 10037, 
     "Name": "Software as a Service", 
     "ParentId": 7 
     }, 
     { 
     "Id": 10038, 
     "Name": "IaaS Component Upgrade", 
     "ParentId": 7 
     }, 
     { 
     "Id": 668, 
     "Name": "CoLo Misc Folder", 
     "ParentId": 7, 
     "items": [ 
      { 
      "Id": 758, 
      "Name": "CoLo: Conduit Fee", 
      "ParentId": 668 
      }, 
      { 
      "Id": 765, 
      "Name": "CoLo: Private VLAN", 
      "ParentId": 668 
      } 
     ] 
     } 
    ] 
    } 
] 
+0

舉一個你想要的結果的例子。目前還不清楚你想要做什麼。 – Amy

+0

我編輯了我的問題以顯示期望的結果。謝謝 – Ganesh

+0

在您的示例JSON中,爲什麼根對象具有'1'的'ParentId'?這是指什麼? –

回答

0

如果使用Json.Net,您可以在使用LINQ-to-JSON API(JObjects)一個通用的方法做這種轉換。這個想法是解析JSON數組並將所有單個項目添加到由Id鍵入的字典中。然後,循環查看字典項目,併爲每個項目嘗試查找父項。如果找到父項,則將該項添加到父項的items數組(如果需要,則創建它)。否則,將該項目添加到root陣列。一路上,從每個項目中刪除depth屬性,因爲您似乎不希望在輸出中。最後,只需將root數組轉儲爲字符串即可獲得最終結果。

var dict = JArray.Parse(json) 
    .Children<JObject>() 
    .ToDictionary(jo => (string)jo["Id"], jo => new JObject(jo)); 

var root = new JArray(); 

foreach (JObject obj in dict.Values) 
{ 
    JObject parent; 
    string parentId = (string)obj["ParentId"]; 
    if (parentId != null && dict.TryGetValue(parentId, out parent)) 
    { 
     JArray items = (JArray)parent["items"]; 
     if (items == null) 
     { 
      items = new JArray(); 
      parent.Add("items", items); 
     } 
     items.Add(obj); 
    } 
    else 
    { 
     root.Add(obj); 
    } 

    JProperty depth = obj.Property("depth"); 
    if (depth != null) depth.Remove(); 
} 

Console.WriteLine(root.ToString()); 

小提琴:https://dotnetfiddle.net/Buza6T

+0

很高興我可以提供幫助。 –

+0

Brian - 代碼在ParentId可用時工作,但ParentId爲空時,我得到一個異常無法將Null轉換爲Int32。 因此我改變了字典對象的不是int 變種字典= JArray.Parse(JSONString) 。兒童() .ToDictionary(JO =>(對象)喬[IdFieldName],J o =>新JObject( JO)); – Ganesh

+0

我添加了一個修復程序,在嘗試使用它之前檢查ParentId是否有值。 –

0

您可以使用JSON.Net動態對象,像這樣來檢測你的屬性動態,那麼你可以建立與所期望的嵌套一個新的JSON對象: 使用Newtonsoft.Json; 使用Newtonsoft.Json.Linq;

  dynamic d = JArray.Parse(stringy); 
      foreach(var ob in d) 
      { 
       if(ob.ParentID != ob.Id) 
       { 
        string debug = "oh snapple, it's a child object"; 
       } 
      }