在python中使用for循环生成动态嵌套JSON

时间:2017-05-30 06:20:08

标签: python json dictionary dynamically-generated

我是Python的新手。我在使用python中的for循环生成嵌套JSON时遇到了一些困难。为了生成嵌套的JSON,我在运行时获得了字典的长度,并根据我想要生成嵌套JSON的字典长度。例如。我得到的字典长度是4.字典长度可能会有所不同。这是我的data_dict词典:

data_dict = {"PHOTO_1" : {"key1" : "PHOTO_2", "key2" : "PHOTO_3", "key3" : "PHOTO_4"}, "PHOTO_2" : {"key1" : "PHOTO_1", "key2" : "PHOTO_3"},"PHOTO_3" : {"key1" : "PHOTO_2"},"PHOTO_4" : {"key1" : "PHOTO_1", "key2" : "PHOTO_2", "key3" : "PHOTO_3"}}

预期结果:

{
    "Requests": [
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_1"
                },
                "connections": {
                    "target": {
                        "id": "PHOTO_2"
                    }
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_1"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_3"
                    }
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_1"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_4"
                    }
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_2"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_1"
                    },
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_2"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_3"
                    },
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_3"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_2"
                    },
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_4"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_1"
                    },
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_4"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_2"
                    },
                }
            },
            "updateData": "connections"
        },
        {
            "photo": {
                "photoId": {
                    "id": "PHOTO_4"
                },
                "connections": {

                    "target": {
                        "id": "PHOTO_3"
                    },
                }
            },
            "updateData": "connections"
        }
    ]
}

请帮忙。我没有得到如何解决这个问题?请不要将其标记为重复。我已经检查了所有答案,我的JSON查询完全不同。

1 个答案:

答案 0 :(得分:2)

使用itertools.permutations()函数的解决方案:

import itertools, json

data_dict = {"first_photo" : "PHOTO_1", "second_photo" : "PHOTO_2", "Thrid" : "PHOTO_3"}
result = {"Requests":[]}

for pair in sorted(itertools.permutations(data_dict.values(), 2)):
    result["Requests"].append({"photo":{"photoId":{"id": pair[0]},
                                        "connections":{"target":{"id": pair[1]}}},"updateData": "connections"})

print(json.dumps(result, indent=4))

新输入词典的附加方法:

data_dict = {"PHOTO_1" : {"key1" : "PHOTO_2", "key2" : "PHOTO_3", "key3" : "PHOTO_4"}, "PHOTO_2" : {"key1" : "PHOTO_1", "key2" : "PHOTO_3"},"PHOTO_3" : {"key1" : "PHOTO_2"},"PHOTO_4" : {"key1" : "PHOTO_1", "key2" : "PHOTO_2", "key3" : "PHOTO_3"}}
result = {"Requests":[]}

for k,d in sorted(data_dict.items()):
    for v in sorted(d.values()):
        result["Requests"].append({"photo":{"photoId":{"id": k},
                                        "connections":{"target":{"id": v}}},"updateData": "connections"})

print(json.dumps(result, indent=4))