如何在C#中将JSON字符串格式化为树格式

时间:2018-12-03 14:56:46

标签: c# .net json tree

我有来自Mongo数据库的数据列表。内容为JSON格式。它是一个对象数组,值彼此之间具有父级关系。父级为null(“”)的对象表示它是根节点。

[  
   {  
      "_id":"5c04fc163838b0772dd9636d",
      "Company":"TESTCOMPANY",
      "id":"test_uk",
      "parent":""
   },
   {  
      "_id":"5c05181f0ab89a44a969015d",
      "Company":"TESTCOMPANY",
      "id":"Gateway",
      "parent":"test_uk"
   },
   {  
      "_id":"5c0518723838b0772dd9678e",
      "Company":"TESTCOMPANY",
      "id":"Device1",
      "parent":"Gateway"
   },
{  
      "_id":"5c0518723838b077789636e",
      "Company":"TESTCOMPANY",
      "id":"Device2",
      "parent":"Gateway"
   },
   {  
      "_id":"5c0518723838b0772dd9636e34",
      "Company":"TESTCOMPANY",
      "id":"Adapter",
      "parent":"test_uk"
   },
      {  
      "_id":"5c0518723838b0772dd9636e",
      "Company":"TESTCOMPANY",
      "id":"AdapterDevice",
      "parent":"Adapter"
   },

   {  
      "_id":"5c04fc163838b0772dd93454d",
      "Company":"TESTCOMPANY",
      "id":"test_us",
      "parent":""
   },
   {  
      "_id":"5c0518723838b0772dd9636e",
      "Company":"TESTCOMPANY",
      "id":"Device",
      "parent":"test_us"
   }
]

是否可以在c#中转换为父子格式?

预期格式

[  
   {  
      "_id":"5c04fc163838b0772dd9636d",
      "Company":"TESTCOMPANY",
      "id":"test_uk",
      "children":[
             {  
                 "_id":"5c05181f0ab89a44a969015d",
                  "Company":"TESTCOMPANY",
                  "id":"Gateway",
                  "children":[
                      {  
                       "_id":"5c0518723838b0772dd9678e",
                       "Company":"TESTCOMPANY",
                       "id":"Device1"
                      },
                     {  
                      "_id":"5c0518723838b077789636e",
                      "Company":"TESTCOMPANY",
                      "id":"Device2"
                       }
                    ]
               },
        {  
           "_id":"5c0518723838b0772dd9636e34",
           "Company":"TESTCOMPANY",
           "id":"Adapter",
        "children":[  {  
             "_id":"5c0518723838b0772dd9636e",
             "Company":"TESTCOMPANY",
             "id":"AdapterDevice"
            }]
       }]},
     {  
      "_id":"5c04fc163838b0772dd93454d",
      "Company":"TESTCOMPANY",
      "id":"test_us",
  "children":[{
      "_id":"5c0518723838b0772dd9636e",
      "Company":"TESTCOMPANY",
      "id":"Device",
      "parent":"test_us"
     }]
    }
]

我只想在Web Ui中显示这种格式的树strucutre

1 个答案:

答案 0 :(得分:0)

我不确定它是否足够快,但是它可能是一种解决方案:

using System.Collections.Generic;
using Newtonsoft.Json;

namespace ConsoleApp1
{
    class Program
    {
        public class Item
        {
            [JsonProperty("_id")]
            public string Guid { get; set; }

            [JsonProperty(PropertyName = "company")]
            public string Company { get; set; }

            [JsonProperty(PropertyName = "id")]
            public string Id { get; set; }

            [JsonProperty(PropertyName = "parent")]
            public string Parent { get; set; }

            [JsonProperty(PropertyName = "children")]
            public List<Item> Children { get; set; }
        }


        private static string jsonString = "[{\"_id\":\"5c04fc163838b0772dd9636d\",\"Company\":\"TESTCOMPANY\",\"id\":\"test_uk\",\"parent\":\"\"},{\"_id\":\"5c05181f0ab89a44a969015d\",\"Company\":\"TESTCOMPANY\",\"id\":\"Gateway\",\"parent\":\"test_uk\"},{\"_id\":\"5c0518723838b0772dd9678e\",\"Company\":\"TESTCOMPANY\",\"id\":\"Device1\",\"parent\":\"Gateway\"},{\"_id\":\"5c0518723838b077789636e\",\"Company\":\"TESTCOMPANY\",\"id\":\"Device2\",\"parent\":\"Gateway\"},{\"_id\":\"5c0518723838b0772dd9636e34\",\"Company\":\"TESTCOMPANY\",\"id\":\"Adapter\",\"parent\":\"test_uk\"},{\"_id\":\"5c0518723838b0772dd9636e\",\"Company\":\"TESTCOMPANY\",\"id\":\"AdapterDevice\",\"parent\":\"Adapter\"},{\"_id\":\"5c04fc163838b0772dd93454d\",\"Company\":\"TESTCOMPANY\",\"id\":\"test_us\",\"parent\":\"\"},{\"_id\":\"5c0518723838b0772dd9636e\",\"Company\":\"TESTCOMPANY\",\"id\":\"Device\",\"parent\":\"test_us\"}]";

        static void Main(string[] args)
        {
            var items =  JsonConvert.DeserializeObject<List<Item>>(jsonString);

            var dictionary = new Dictionary<string, Item>();

            foreach (var item in items)
            {
                if (!dictionary.ContainsKey(item.Parent))
                {   
                    dictionary.Add(item.Id, item);
                }
                else
                {
                    if (dictionary[item.Parent].Children == null)
                        dictionary[item.Parent].Children = new List<Item>();
                    dictionary[item.Parent].Children.Add(item);
                }
            }

            string json = JsonConvert.SerializeObject(dictionary, Formatting.Indented);

            System.Console.WriteLine(json);

            System.Console.ReadLine();
        }
    }
}

我刚刚制作了一个使用Newtonsoft.Json库的简单控制台应用程序。我认为它是最常用于这种目的的

您必须自行找到如何删除结果中的父级属性,但我想这应该没什么大不了的。