使用python在json文件中插入一个值

时间:2016-08-03 08:46:55

标签: python json

对于一个项目,我需要使用json存储一些值:

{
"Serial_011": "011",
"Servers_011":
    [
        {
            "hostname": "srv-a.11",
            "ipv4_address": "0.0.0.0",
            "services":
                [
                    {
                        "uri": "http://www.google.fr/1",
                        "expected_code": 200
                    },
                    {
                        "uri": "http://www.google.fr/2",
                        "expected_code": 200
                    }
                ]
        },
        {
            "hostname": "nsc-srv-b.11",
            "ipv4_address": "0.0.0.0",
            "services":
                [
                    {
                        "uri": "http://www.google.fr/3",
                        "expected_code": 200
                    },
                    {
                        "uri": "http://www.google.fr/4",
                        "expected_code": 200
                    }
                ]
        }
    ]
}

我想用这种格式添加服务:

{ "uri": "http://www.google.fr" "expected_code": 200 }

所以它将具有以下格式:

{
"Serial_011": "011",
"Servers_011":
    [
        {
            "hostname": "srv-a.11",
            "ipv4_address": "0.0.0.0",
            "services":
                [
                    {
                        "uri": "http://www.google.fr/1",
                        "expected_code": 200
                    },
                    {
                        "uri": "http://www.google.fr/2",
                        "expected_code": 200
                    },
                    {
                        "uri": "http://www.google.fr"
                        "expected_code": 200
                    }
                ]
        },
        {
            "hostname": "nsc-srv-b.11",
            "ipv4_address": "0.0.0.0",
            "services":
                [
                    {
                        "uri": "http://www.google.fr/3",
                        "expected_code": 200
                    },
                    {
                        "uri": "http://www.google.fr/4",
                        "expected_code": 200
                    }
                ]
        }
    ]
}

但是我不能这样做...我尝试使用.append(),但是它在本地,json.dumps()但是我不能比第一个缩进更远...

这是我尝试使用.append():

with open('Example_of_configuration', 'r') as f:
    dic = json.load(f)
    dic['Servers_011'][0]['services'].append('{"uri":"http://www.google.com", "expected_code": 200}')

有人知道该怎么办?

提前致谢

3 个答案:

答案 0 :(得分:0)

您要附加字符串值而不是字典。而不是:

dic['Servers_011'][0]['services'].append('{"uri":"http://www.google.com", "expected_code": 200}')

待办事项

dic['Servers_011'][0]['services'].append({"uri":"http://www.google.com", "expected_code": 200})  # as a dict with two keys, without quotes

或者像旧版本一样:

dic['Servers_011'][0]['services'].append(structure)

(不含引号)

dic为例:

>>> dic['Servers_011'][0]['services']  # before
[{'expected_code': 200, 'uri': 'http://www.google.fr/1'}, {'expected_code': 200, 'uri': 'http://www.google.fr/2'}]
>>> dic['Servers_011'][0]['services'].append({"uri":"http://www.google.com", "expected_code": 200})
>>> dic['Servers_011'][0]['services']  # after
[{'expected_code': 200, 'uri': 'http://www.google.fr/1'}, {'expected_code': 200, 'uri': 'http://www.google.fr/2'}, {'expected_code': 200, 'uri': 'http://www.google.com'}]
>>>

然后你可以(过)把它写回同一个文件,按照@ julien-spronck的回答:

with open('test.json', 'w') as f:
    json.dump(dic, f)

答案 1 :(得分:0)

with open('test.json', 'r') as f:
    dic = json.load(f)
    dic['Servers_011'][0]['services'].append({"uri": "http://www.google.com", "expected_code": 200})

with open('test.json', 'w') as f:
    json.dump(dic, f)

答案 2 :(得分:0)

你正在做正确的事。但问题就像传递一个字符串一样。需要做一些小改动。只需在传递结构时删除单引号。 最终你想以dict的形式,但你是作为一个字符串传递。这显然是不可能的。请参阅以下内容:

with open('Example_of_configuration', 'r') as f:
    dic = json.load(f)
    dic['Servers_011'][0]['services'].append({"uri":"http://www.google.com", "expected_code": 200})

我希望它能奏效。