求一个分组的算法

2019-11-07 14:11发布

现有数据:

string[] list = [
    "a1:b1:c1",
    "a1:b2:c2:d1:",
    "a2:b2:c2:d2"
    ...
];

需要按":"符号分割成如下格式的JSON

[
    {
        "name": "a1",
        "child": [
            {
                "name": "b1",
                "child": [
                    {
                        "name": "c1",
                        "child": []
                    }
                ]
            },
            {
                "name": "b2",
                "child": [
                    {
                        "name": "c2",
                        "child": [
                            {
                                "name": "d1",
                                "child": []
                            },
                            {
                                "name": "d2",
                                "child": []
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

求大神支招。。。

标签:
6条回答
何必那么认真
2楼-- · 2019-11-07 14:41
lis = [
    "a1:b1:c1",
    "a1:b2:c2:d1:",
    "a2:b2:c2:d2"
]
new_lis =[]
for str in lis:
    str_lis = str.split(':')
    dic ={}
    start_index = 0
    def func(dic,start_index):
            if start_index==len(str_lis):return
            dic['name']= str_lis[start_index]
            start_index+=1
            dic ['child']={}
            func(dic['child'],start_index)
    func(dic,start_index)
    new_lis.append(dic)
print(new_lis)

查看更多
走好不送
3楼-- · 2019-11-07 14:45
[Test]
public void Test1()
{
    var dataList = new[] {"a1:b1:c1", "a1:b2:c2:d1:", "a2:b2:c2:d2"};
    var root = new List<dynamic>();
    var dict = new Dictionary<string, dynamic>();//字典,方便搜索node
    foreach (var data in dataList)
    {
        dynamic parent = null;
        foreach (var name in data.Split(':'))
        {
            dict.TryGetValue(name, out var current);
            if (current != null)
            {
                parent = current;
                continue;
            }

            var node = new {name, child = new List<dynamic>()};
            if (parent == null)
            {
                root.Add(node);
            }
            else
            {
                parent.child.Add(node);
            }

            parent = node;
            dict.Add(name, node);
        }
    }

    Console.WriteLine(DefaultJsonSerializer.Instance.SerializeObject(root));
}
[{"name":"a1", "child":[{"name":"b1", "child":[{"name":"c1", "child":[]}]},{"name":"b2", "child":[{"name":"c2", "child":[{"name":"d1", "child":[{"name":"", "child":[]}]},{"name":"d2", "child":[]}]}]}]},{"name":"a2", "child":[]}]
查看更多
走好不送
4楼-- · 2019-11-07 14:47

集合中的元素 长度也不是固定的吗? 如:"a1:b2:c2:d1:"

查看更多
Ridiculous、
5楼-- · 2019-11-07 14:48

list.Select(t=>t.Split(":")).Select(t=>new {

name=t[0],

childre=new List{

  new {。。。}

  }

})

一行代码的事,要什么递归。

 

//Modify 不好意思 没注意是字符串,字符串更简单,把第二Select 做字符串格式化一下,再用Aggregate连接一下 

查看更多
冷血范
6楼-- · 2019-11-07 14:56

数据量多的话直接用map好了 效率更高些

查看更多
叛逆
7楼-- · 2019-11-07 14:59

用递归三生三世

查看更多
登录 后发表回答