如何从python脚本中读取json文件中的列表

时间:2019-01-09 09:13:29

标签: python json api

问题:1 赞赏有人可以进一步建议我。我想使用python脚本和netmiko模块将其SSH到我的设备。我通过api请求获取ssh详细信息(json文件),并使用数据来运行脚本。但是我在下面出现错误。我希望有人能给我建议并给我指路。

我在这里错过了一些东西,但是我不知道如何解决。请纠正并引导我。谢谢。

json文件如下创建;

response = requests.post(url,data=data,headers=headers,verify=False)
dictionary_info = response.json() 
with open('devices.json', 'w') as fp:
  json.dump(dictionary_info, fp, indent=4, sort_keys=True)

devices.json文件如下;

{
"device": [
     {
        "login": "test1",
        "ip": "10.10.10.1",
        "password": "test1",
        "device_type": "cisco_ios"
    },
    {
        "login": "test1",
        "ip": "10.10.10.2",
        "password": "test1",
        "device_type": "cisco_ios"
    },
    {
        "login": "test1",
        "ip": "10.10.10.3",
        "password": "test1",
        "device_type": "cisco_ios"
    },
    {
        "login": "test1",
        "ip": "10.10.10.4",
        "password": "test1",
        "device_type": "cisco_ios"
    }
],
"status": "SUCCESS"
}

当我运行下面的脚本(代码段)时,它将在下面返回错误

File "devices.py", line 18, in <module>
print('Connecting to device:',device['ip'])
TypeError: string indices must be integers

for device in devices:
try:
    print('~' * 79)
    print('Connecting to device:',device['ip'])
    connection = netmiko.ConnectHandler(**device)
    print(connection.send_command('show interface'))
    connection.disconnect()
except netmiko_exceptions as e:
    print('Failed to ', device['ip'], e)

我应该能够根据json文件提供的ssh详细信息运行脚本并ssh到每个设备。我不知道如何以某种方式需要进一步修改代码。请帮我。谢谢


我编辑devices.json的内容。删除大括号{},对象名称“ device”和“ status”,然后仅保留列表[],如下所示;

   [
    {
        "username": "scnpa",
        "ip": "10.10.10.1",
        "password": "123",
        "device_type": "cisco_ios"
    },
    {
        "username": "scnpa",
        "ip": "10.10.10.2",
        "password": "123",
        "device_type": "cisco_ios"
    },
    {
        "username": "scnpa",
        "ip": "10.10.10.3",
        "password": "123",
        "device_type": "cisco_ios"
    },
    {
        "username": "scnpa",
        "ip": "10.10.10.4",
        "password": "123",
        "device_type": "cisco_ios"
    }
   ]

今天,我只是尝试n错误...然后运行脚本,它可以工作..使用上面的可编辑json文件...我知道这不是解决方案,因为最终我将获得我之前共享的json文件(使用大括号{}等)还是有办法,我可以像上面的格式[{},{},{},{}]?从json api响应创建文件,仅获得{{},{}。{},{}]内容...?

或保持原样...

可能需要在for循环部分修改python脚本。我尝试删除设备,但遇到了其他错误。将“设备”更改为其他名称,仍然出现错误“ TypeError:字符串索引必须为整数”。 不知道...

for device in devices:
try:
    print('~' * 79)
    print('Connecting to device:',device['ip'])
    connection = netmiko.ConnectHandler(**device)
    print(connection.send_command('show interface'))
    connection.disconnect()


问题:2我创建了单独的帖子

关于从API读取响应json文件(字符串)查询,我还有另一个问题。例如json响应如下

{
"status": "SUCCESS",
"device": [
    {
        "model":"XXXX-A",
        "username": "scnpa1",
        "ip": "10.10.10.1",
        "password": "123",
        "device_type": "cisco_ios"
    },
    {
        "model":"XXXX-A",
        "username": "scnpa2",
        "ip": "10.10.10.2",
        "password": "456",
        "device_type": "cisco_ios"
    }
]

}

如何仅确保特定的键/值(例如名称和密码)并能够将文件打印和创建为json文件(列表),并可以通过python脚本读取为输入。希望下面会这样

{
"status": "SUCCESS",
"device": [
    {
        "username": "scnpa1",
        "ip": "10.10.10.1",
        "password": "123"
    },
    {
        "username": "scnpa2",
        "ip": "10.10.10.2",
        "password": "456"
    }
]

}

我使用下面的代码,但是它将使用所有参数创建json文件

response = requests.post(url,data=data,headers=headers,verify=False)
dictionary_info = response.json() 
with open('devices.json', 'w') as fp:
  json.dump(dictionary_info, fp, indent=4, sort_keys=True)

请告诉我。谢谢

1 个答案:

答案 0 :(得分:0)

您应该将循环更改为

[...]