在python中将父子关系转换为Json

时间:2011-09-23 03:27:25

标签: python json parent

我有一个列表,如下所示。第一列是父级,第二列是子级,第三列是节点属性。我需要将以下内容转换为JSON格式,如下所示。

0 0 "flair" 1000
0 1 "analytics" 1000

1 2 "cluster" 1000
2 3 "AgglomerativeCluster" 1000
2 4 "CommunityStructure" 1000

1 5 "Graph" 1000
5 6 "BetweennessCentrality" 1000
5 7 "LinkDistance"

pc = []
pc.append([0, 0 ,"flair", 1000])
pc.append([0,1, "analytics", 1000])
pc.append([1, 2, "cluster", 1000])
pc.append([2 ,3, "AgglomerativeCluster", 1000])
pc.append([2 ,4, "CommunityStructure" ,1000])
pc.append([1 ,5, "Graph", 1000])
pc.append([5, 6, "BetweennessCentrality", 1000])
pc.append([5, 7, "LinkDistance",1000])

{
 "name": "flare",
 "children": [
  {
   "name": "analytics",
   "children": [
    {
     "name": "cluster",
     "children": [
      {"name": "AgglomerativeCluster", "size": 3938},
      {"name": "CommunityStructure", "size": 3812},
     ]
    },
    {
     "name": "graph",
     "children": [
      {"name": "BetweennessCentrality", "size": 3534},
      {"name": "LinkDistance", "size": 5731}
     ]
    }
   ]
  }
 ]
}

2 个答案:

答案 0 :(得分:2)

对您的输入稍有改动,对于根节点"天赋",我使用' -1'作为其父ID,而不是' 0'。

import json
pc = []
pc.append([-1, 0 ,"flair", 1000])
pc.append([0,1, "analytics", 1000])
pc.append([1, 2, "cluster", 1000])
pc.append([2 ,3, "AgglomerativeCluster", 1000])
pc.append([2 ,4, "CommunityStructure" ,1000])
pc.append([1 ,5, "Graph", 1000])
pc.append([5, 6, "BetweennessCentrality", 1000])
pc.append([5, 7, "LinkDistance",1000])

def listToDict(input):
    root = {}
    lookup = {}
    for parent_id, id, name, attr in input:
        if parent_id == -1: 
            root['name'] = name;
            lookup[id] = root
        else:
            node = {'name': name}
            lookup[parent_id].setdefault('children', []).append(node)
            lookup[id] = node
    return root

result = listToDict(pc)
print result
print json.dumps(result)

答案 1 :(得分:1)

不确定那些'size'属性来自哪里,它们不出现在'pc'列表中,但假设它们是从每个列表中的第4项中获取的,并且它们在输出树中都应该是1000 ,这应该工作

def make_tree(pc_list):
    results = {}
    for record in pc_list:
        parent_id = record[0]
        id = record[1]

        if id in results:
            node = results[id]
        else:
            node = results[id] = {}

        node['name'] = record[2]
        node['size'] = record[3]
        if parent_id != id:
            if parent_id in results:
                parent = results[parent_id]
            else:
                parent = results[parent_id] = {}
            if 'children' in parent:                
                parent['children'].append(node)
            else:
                parent['children'] = [node]        

    # assuming we wanted node id #0 as the top of the tree          
    return results[0]  

相当打印make_tree(pc)的输出我得到了

{'children': [{'children': [{'children': [{'name': 'AgglomerativeCluster',
                                           'size': 1000},
                                          {'name': 'CommunityStructure',
                                           'size': 1000}],
                             'name': 'cluster',
                             'size': 1000},
                            {'children': [{'name': 'BetweennessCentrality',
                                           'size': 1000},
                                          {'name': 'LinkDistance',
                                           'size': 1000}],
                             'name': 'Graph',
                             'size': 1000}],
               'name': 'analytics',
               'size': 1000}],
 'name': 'flair',
 'size': 1000}

虽然键显示的顺序不同,但几乎就像你的样本输出(大小值除外)