JSON的Python列表(节点id、父节点id)

JSON的Python列表(节点id、父节点id),python,json,Python,Json,我有一个结构如下的列表: [(节点id,父节点id),…(节点id,父节点id)] 例如: data = [(1, None), (4, 1), (15, 1), (6, 1), (2, 1), (7, 1), (12, 7), (13, 7), (17, 13),

我有一个结构如下的列表: [(节点id,父节点id),…(节点id,父节点id)]

例如:

data = [(1, None),
            (4, 1),
            (15, 1),
            (6, 1),
            (2, 1),
            (7, 1),
                (12, 7),
                (13, 7),
                    (17, 13),
                        (18, 17),
                (14, 7),
            (8, 1),
            (9, 1),
            (10, 1),
            (11, 1),
            (19, 1),
        (16, None)]
如何将此列表转换为JSON嵌套

UPD:结果是这样的

{

    1:{
        4:'',
        15:'',
        6:'',
        2:'',
        7:{
            12:'',
            13:{
                17:{
                    18:''
                }
            },
            14:'',
        },
        8:'',
        9:'',
        10:'',
        11:'',
        19:'',
    },
    16:''

}
使用或:


您确实需要检查您的系统需求。元组列表不能很好地满足您要完成的任务。但是,如果您被一些您无法控制的外部需求所锁定,那么下面是一个示例解决方案

@falsetru是正确的,您将需要使用JSON.dumps,但只有在您将输入数据转换为所需的输出格式之后

import json
data = [(1, None),
            (4, 1),
            (15, 1),
            (6, 1),
            (2, 1),
            (7, 1),
                (12, 7),
                (13, 7),
                    (17, 13),
                        (18, 17),
                (14, 7),
            (8, 1),
            (9, 1),
            (10, 1),
            (11, 1),
            (19, 1),
        (16, None)]

def convert(input):
    loading_struct = {} #load your tuples into a dict object (because I like dict)
    alignment_struct = {} #structure to hold the output
    seen_embedded_keys = {} #keep track of items we have seen before to remove them
    for line in input: #iterating your input of a list of tuples
        loading_struct[line[0]] = line[1] #unloading them unto a dictionary
        alignment_struct[line[0]] = {} #creating a blank result dictionary with your proposed output
    for node_id, parent_node_id in loading_struct.items(): #iterating the loading struct
        if parent_node_id: #if it doesnt have a parent, we dont need to do anything
            alignment_struct[parent_node_id][node_id] = alignment_struct[node_id]
            seen_embedded_keys[node_id] = True
    for node_id in seen_embedded_keys: #cleanup time remove the keys that are embedded somewhere else
        del alignment_struct[node_id]
    return alignment_struct

output = json.dumps(convert(data)).replace('{}', '""') #your requirement to have nodes with no children to have values of ''
print output
将打印上述脚本的输出

{"1": {"2": "", "4": "", "6": "", "7": {"12": "", "13": {"17": {"18": ""}}, "14": ""}, "8": "", "9": "", "10": "", "11": "", "15": "", "19": ""}, "16": ""}

同样,请再次检查您的需求,以首先优化这些需求。

这是正确的,但我认为他的输入与输出无关。注意,他想要嵌套的输出,尽管他的输入形式不是很有用。
{"1": {"2": "", "4": "", "6": "", "7": {"12": "", "13": {"17": {"18": ""}}, "14": ""}, "8": "", "9": "", "10": "", "11": "", "15": "", "19": ""}, "16": ""}