Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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 如何在只有一个字符串的json中修改json,该字符串为';包括';搜索键';_Python_Json - Fatal编程技术网

Python 如何在只有一个字符串的json中修改json,该字符串为';包括';搜索键';

Python 如何在只有一个字符串的json中修改json,该字符串为';包括';搜索键';,python,json,Python,Json,因此,我使用这个函数将值加载到我的config.json 但问题是,此函数只能将值加载到“first”json中。 “first”是指像这样的json def load_value_to_config(name, value): data = json.load(open("config.json")) data[name] = value with open("config.json", "w") as fi

因此,我使用这个函数将值加载到我的
config.json

但问题是,此函数只能将值加载到“first”json中。 “first”是指像这样的
json

def load_value_to_config(name, value):
    data = json.load(open("config.json"))
    data[name] = value

    with open("config.json", "w") as file:
        json.dump(data, file, indent=4)
在这种情况下,我可以这样做

{
    "name": "John"
}
它会变成
'Michael'

但问题是,我的配置文件也有
json
,在
json
中。所以像这样:

load_value_to_config("name", "Michael")
我想继续使用
load\u value\u to\u config()
函数。但我不知道如何修改它,使其也能将值加载到上面
json
中的“first_name”中

所以我会这样做

{
    "name": {
        "first_name": "John",
        "last_name": "Doe",
    }
}
作为输出:

load_value_to_config("name;first_name", "Michael")

我需要它为
json
工作,在
json
内部,在
json
内部。因此,解决方案应该适用于
json

中的多个
json
,在本例中,您正在访问“name”对象中的嵌套元素。请尝试以下方法:

{
    "name": {
        "first_name": "Michael",
        "last_name": "Doe",
    }
}
更新的解决方案:您的意思是这样的吗?在迭代时,最好将数组用作确定级别的参数。希望这有助于:

import json

def load_value_to_config(name, column, value):
    data = json.load(open("config.json"))
    data[name][column] = value

    with open("config.json", "w") as file:
        json.dump(data, file, indent=4)

load_value_to_config("name", "first_name" , "John")

对不起,我刚刚编辑了我的问题。我需要它为json工作,在json内部,在json内部。这样的解决方案是行不通的。我编辑了上面的答案,在字典中加入了动态迭代。可能最好使用数组,也可以使用。如果您更喜欢使用字符串而不是字符串,请使用.split('.')方法将字符串拆分出来array@JuliusJonker你是怎么处理这件事的?运气好吗?请查看我的更新解决方案,并让我知道这是否有帮助:)
import json

data = json.load(open("config.json"))

def load_value_to_config(data, column, value):
    column_levels = len(column)
    columns = data.keys()
    if column_levels > 1:
        if column[0] not in columns:
            data[column[0]] = {}    
        load_value_to_config(data[column[0]], column[1:], value)
    else:
            data[column[0]] = value

    with open("config.json", "w") as file:
        json.dump(data, file, indent=4)
        
load_value_to_config(data, ["name", "last_name"], "Murphy")