Python列表(node_id,parent_node_id)到JSON

时间:2013-12-20 11:30:21

标签: python json

我有一个包含以下结构的列表: [(node_id,parent_node_id),..(node_id,parent_node_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:''

}

2 个答案:

答案 0 :(得分:6)

使用json.dumpsjson.dump

>>> 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)
... ]
>>> import json
>>> json.dumps(data)
'[[1, null], [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, null]]'
>>>

答案 1 :(得分:1)

你真的需要检查你的系统要求。元组列表并不适合您要完成的任务。但是,如果您被某些您无法控制的外部要求所锁定,则此处是一个示例解决方案。

@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": ""}

同样,请仔细检查您的要求,先对其进行优化。