2017-05-12 73 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"; 
       } 
      }