将C#列表转换为层次树



我有一个C#列表,其中包含文件名和该文件所在的文件夹。

public class Data
    {
        public string Name { get; set; }
        public string Folder { get; set; }
    }
    public class Tree
    {
        public List<Data> dataList = new List<Data>();

        public Tree()
        {
            Data d = new Data();
            d.Name = "FireFox";
            d.Folder = @"TestingMac OSX";
            dataList.Add(d);
            d = new Data();
            d.Name = "Safari";
            d.Folder = @"TestingMac OSX";
            dataList.Add(d);
            d = new Data();
            d.Name = "Chrome";
            d.Folder = @"TestingMac OSX";
            dataList.Add(d);
            d = new Data();
            d.Name = "FireFox";
            d.Folder = @"TestingWindows";
            dataList.Add(d);
            d = new Data();
            d.Name = "Safari";
            d.Folder = @"TestingWindows";
            dataList.Add(d);
            d = new Data();
            d.Name = "Chrome";
            d.Folder = @"TestingWindows";
            dataList.Add(d);
            d = new Data();
            d.Name = "Internet Exploder";
            d.Folder = @"TestingWindows";
            dataList.Add(d);

            d = new Data();
            d.Name = "Chrome";
            d.Folder = @"TestingLinux";
            dataList.Add(d);
            d = new Data();
            d.Name = "Firefox";
            d.Folder = @"TestingLinux";
            dataList.Add(d);
            d = new Data();
            d.Name = "Testing First Child";
            d.Folder = @"Testing";
            dataList.Add(d);
            d = new Data();
            d.Name = "First Child";
            d.Folder = null;
            dataList.Add(d);
        }
    }

我需要将这个列表转换为层次json对象。输出应该是这样的:

{
            "text": ".",
            children: [{
                task: 'First Child',
                duration: 6.5,
                user: 'Tommy Maintz',
                leaf: true,
                iconCls: 'task'
            }, {
                task: 'Testing',
                duration: 2,
                user: 'Core Team',
                iconCls: 'task-folder',
                children: [{
                    task: 'Testing First Child',
                    duration: 6.5,
                    user: 'Tommy',
                    leaf: true,
                    iconCls: 'task'
                }, {
                    task: 'Mac OSX',
                    duration: 0.75,
                    user: 'Tommy Maintz',
                    iconCls: 'task-folder',
                    children: [{
                        task: 'FireFox',
                        duration: 0.25,
                        user: 'Tommy Maintz',
                        iconCls: 'task',
                        leaf: true
                    }, {
                        task: 'Safari',
                        duration: 0.25,
                        user: 'Tommy Maintz',
                        iconCls: 'task',
                        leaf: true
                    }, {
                        task: 'Chrome',
                        duration: 0.25,
                        user: 'Tommy Maintz',
                        iconCls: 'task',
                        leaf: true
                    }]
                }, {
                    task: 'Windows',
                    duration: 3.75,
                    user: 'Darrell Meyer',
                    iconCls: 'task-folder',
                    children: [{
                        task: 'FireFox',
                        duration: 0.25,
                        user: 'Darrell Meyer',
                        iconCls: 'task',
                        leaf: true
                    }, {
                        task: 'Safari',
                        duration: 0.25,
                        user: 'Darrell Meyer',
                        iconCls: 'task',
                        leaf: true
                    }, {
                        task: 'Chrome',
                        duration: 0.25,
                        user: 'Darrell Meyer',
                        iconCls: 'task',
                        leaf: true
                    }, {
                        task: 'Internet Exploder',
                        duration: 3,
                        user: 'Darrell Meyer',
                        iconCls: 'task',
                        leaf: true
                    }]
                }, {
                    task: 'Linux',
                    duration: 0.5,
                    user: 'Aaron Conran',
                    iconCls: 'task-folder',
                    children: [{
                        task: 'FireFox',
                        duration: 0.25,
                        user: 'Aaron Conran',
                        iconCls: 'task',
                        leaf: true
                    }, {
                        task: 'Chrome',
                        duration: 0.25,
                        user: 'Aaron Conran',
                        iconCls: 'task',
                        leaf: true
                    }]
                }]
            }]
        }

我想做的是:

  1. 在dataList中的每个项目上循环
  2. 让我们以示例Folder Path : TestingMac OSX中的第一个对象为例
  3. 如果路径不存在,则创建一个新的json节点并添加它
  4. 因此,系统将创建两个节点,第一个用于"测试",第二个用于"Mac OSX"
  5. 现在添加文件名FireFox Mac OSX
  6. 如果节点存在,只需在该节点下添加文件名
  7. 对所有项目重复此操作

是否可以使用JSON.NET库或任何其他库来实现这一点

感谢

将您的结构转换为以下结构,该结构应该能够序列化为您想要的Json结构。

 public class Child
        {
            public string task { get; set; }
            public double duration { get; set; }
            public string user { get; set; }
            public bool leaf { get; set; }
            public string iconCls { get; set; }
            public List<Child> children { get; set; }
        }
        public class Tree
        {
            public string text { get; set; }
            public List<Child> children { get; set; }
        }

这就是我们实现结果的方法

class Node
{
    public Node()
    {
        Children = new List<Node>();
    }
    public string Name { get; set; }
    public List<Node> Children { get; set; }
}

Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Kitchen supplies", "Shopping / Housewares");
dict.Add("Groceries", "Shopping / Housewares");
dict.Add("Cleaning supplies", "Shopping / Housewares");
dict.Add("Office supplies", "Shopping / Housewares");
dict.Add("Retile kitchen", "Shopping / Remodeling");
dict.Add("Ceiling", "Shopping / Remodeling / Paint bedroom");
dict.Add("Walls", "Shopping / Remodeling / Paint bedroom");
dict.Add("Misc", null);
dict.Add("Other", "Shopping / Remodeling");
Node root = new Node();
foreach (KeyValuePair<string, string> kvp in dict)
{
    Node parent = root;
    if (!string.IsNullOrEmpty(kvp.Value))
    {
        Node child = null;
        foreach (string part in kvp.Value.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries))
        {
            string name = part.Trim();
            child = parent.Children.Find(n => n.Name == name);
            if (child == null)
            {
                child = new Node { Name = name };
                parent.Children.Add(child);
            }
            parent = child;
        }
    }
    parent.Children.Add(new Node { Name = kvp.Key });
}
//Using JSON.NET
 string output = JsonConvert.SerializeObject(root);

相关内容

  • 没有找到相关文章

最新更新