Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用for循环生成动态嵌套JSON_Python_Json_Dictionary_Dynamically Generated - Fatal编程技术网

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

在python中使用for循环生成动态嵌套JSON,python,json,dictionary,dynamically-generated,Python,Json,Dictionary,Dynamically Generated,我是Python的新手。我在python中使用for循环生成嵌套JSON时遇到一些困难。为了生成嵌套的JSON,我在运行时得到了字典的长度,并根据字典的长度生成了嵌套的JSON。我得到的词典长度是4。词典的长度可能会有所不同。这是我的数据字典: data_dict = {"PHOTO_1" : {"key1" : "PHOTO_2", "key2" : "PHOTO_3", "key3" : "PHOTO_4"}, "PHOTO_2" : {"key1" : "PHOTO_1", "key2"

我是Python的新手。我在python中使用for循环生成嵌套JSON时遇到一些困难。为了生成嵌套的JSON,我在运行时得到了字典的长度,并根据字典的长度生成了嵌套的JSON。我得到的词典长度是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"}}
预期结果:

{
    "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查询完全不同。

使用函数的解决方案:

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))

的可能重复项是否词典始终长度
3
?@RomanPerekhrest否,词典长度未固定。它是不同的。您的json无效,您不能有相同的
目标
keys@ArunaRajput,你有我的解决办法吗