【问题标题】:Python list of (node_id, parent_node_id) to JSON(node_id, parent_node_id) 到 JSON 的 Python 列表
【发布时间】:2013-12-21 00:58:03
【问题描述】:

我有一个具有以下结构的列表: [(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:''

}

【问题讨论】:

    标签: python json


    【解决方案1】:

    使用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]]'
    >>>
    

    【讨论】:

    • 这是正确的,但我认为他的输入与他的输出无关。请注意,他想要嵌套输出,尽管他的输入并没有采用非常有用的形式。
    【解决方案2】:

    您确实需要检查您的系统要求。元组列表不适合您要完成的工作。但是,如果您被一些您无法控制的外部要求锁定,这里有一个示例解决方案。

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

    再次,请仔细检查您的要求以首先优化这些要求。

    【讨论】:

      猜你喜欢
      • 2021-04-02
      • 2018-07-13
      • 2014-01-10
      • 2014-11-19
      • 2018-05-12
      • 2018-07-19
      • 2018-01-15
      • 2019-01-13
      • 2013-02-23
      相关资源
      最近更新 更多