将C#欲望转换为分层树

时间:2013-12-05 13:04:16

标签: c# json data-structures tree json.net

我有一个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 = @"Testing\Mac OSX";
            dataList.Add(d);

            d = new Data();
            d.Name = "Safari";
            d.Folder = @"Testing\Mac OSX";
            dataList.Add(d);

            d = new Data();
            d.Name = "Chrome";
            d.Folder = @"Testing\Mac OSX";
            dataList.Add(d);

            d = new Data();
            d.Name = "FireFox";
            d.Folder = @"Testing\Windows";
            dataList.Add(d);

            d = new Data();
            d.Name = "Safari";
            d.Folder = @"Testing\Windows";
            dataList.Add(d);

            d = new Data();
            d.Name = "Chrome";
            d.Folder = @"Testing\Windows";
            dataList.Add(d);

            d = new Data();
            d.Name = "Internet Exploder";
            d.Folder = @"Testing\Windows";
            dataList.Add(d);


            d = new Data();
            d.Name = "Chrome";
            d.Folder = @"Testing\Linux";
            dataList.Add(d);

            d = new Data();
            d.Name = "Firefox";
            d.Folder = @"Testing\Linux";
            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. 中的每个项目
  3. 让我们在示例Folder Path : Testing\Mac OSX
  4. 中获取第一个对象
  5. 如果路径不存在,请创建一个新的json节点并添加它
  6. 因此系统将创建两个节点首先用于“测试”并在其下面创建“Mac OSX”
  7. 现在添加文件名FireFox Mac OSX
  8. 如果该节点存在,只需在该节点下添加文件名。
  9. 对所有项目重复此操作
  10. 是否可以使用JSON.NET库或任何其他库实现此目的?

    由于

2 个答案:

答案 0 :(得分:0)

将您的结构转换为以下结构,并且应该能够序列化为您所追求的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; }
        }

答案 1 :(得分:0)

这就是我们如何实现结果

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);