1

大家好,我正在为此工作几个小时,但似乎我无法完成这项工作。

我有这个json结构:

{
    "1": {
        "name": "foo",
        "color": "black",
        "children": ["2", "3"]
     },
    "2": {
        "name": "foo2",
        "color": "green",
        "children": []
     },
    "3": {
        "name": "foo3",
        "color": "yellow",
        "children": ["4"]
     },
    "4": {
        "name": "foo4",
        "color": "purple",
        "children": []
     }
}

我想使用python字典在下一个json结构中转换它:

{
    "foo":{
        "color":"black",
        "children":{
            "foo2":{
                "color":"green",
                "children":{}
            },
            "foo3":{ 
                "color":"yellow",
                "children":{
                    "foo4":{
                        "color":"purple", 
                        "children":{}
                        }
                }
            }
        }
    }
}

有人能帮助我吗?

4

2 回答 2

1

不需要递归。试试这个(s作为你的原始字符串):

>>> import json
>>> data = json.loads(s)
>>> for v in data.values():
    v['children'] = {data[c]['name']:data[c] for c in v['children']}


>>> d = {data['1']['name']:data['1']}
>>> for v in data.values():
    del v['name']


>>> print(json.dumps(d, indent=4))
{
    "foo": {
        "color": "black", 
        "children": {
            "foo2": {
                "color": "green", 
                "children": {}
            }, 
            "foo3": {
                "color": "yellow", 
                "children": {
                    "foo4": {
                        "color": "purple", 
                        "children": {}
                    }
                }
            }
        }
    }
}

因此,第一次传递数据以替换指向实际孩子的“指针”,然后第二次传递以摆脱名称。所有的字典/列表都在适当的位置发生了变化,因此链接的孩子继续工作。

于 2013-02-05T12:12:07.507 回答
0

首先,您必须知道 dict 没有排序,因此您无法知道“1”将是您在迭代时获得的第一个键。如果总是“1”是第一个元素,那么:

dict_to_convert = {
    "1": {
        "name": "foo",
        "color": "black",
        "children": ["2", "3"]
    },
    "2": {
        "name": "foo2",
        "color": "green",
        "children": []
    },
    "3": {
        "name": "foo3",
        "color": "yellow",
        "children": ["4"]
    },
    "4": {
        "name": "foo4",
        "color": "purple",
        "children": []
    }
}


def convert_dict(dict_to_convert, key=None):
    if key is not None:
        new_dict = {}
        for key in dict_to_convert[key]["children"]:
            new_dict[dict_to_convert[key]["name"]] = {}
            new_dict[dict_to_convert[key]["name"]]["color"] = dict_to_convert[key]["color"]
            new_dict[dict_to_convert[key]["name"]]["children"] = convert_dict(dict_to_convert, key)
    else:
        new_dict = {}
        new_dict[dict_to_convert["1"]["name"]] = {}
        new_dict[dict_to_convert["1"]["name"]]["color"] = dict_to_convert["1"]["color"]
        new_dict[dict_to_convert["1"]["name"]]["children"] = convert_dict(dict_to_convert, "1")
    return new_dict


converted_dict = convert_dict(dict_to_convert)
print converted_dict

我检查了它,它有效。

于 2013-02-05T12:35:54.310 回答