【问题标题】:How to grab value in data from json resonse?如何从 json 响应中获取数据中的值?
【发布时间】:2021-10-28 11:47:47
【问题描述】:

问:如何从我的 api 响应中获取值?

我的代码:

#!/usr/bin/python3

API_KEY = ""
ORG_ID = ""

import meraki
import requests
import json
import time
import sys
from pprint import pprint

url = "https://api.meraki.com/api/v1/organizations/""/appliance/uplink/statuses"

payload = {}
headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json',
  'X-Cisco-Meraki-API-Key': (API_KEY)
}

response = requests.request('GET', url, headers=headers, data = payload)
pprint(response.json())

回复:

{'networkId': 'A_1234567890', 
'serial': 'abcd-1234-abcd', 
'model': 'MXYZ', 
'lastReportedAt': '2021-01-01T00:00:00Z', 
'uplinks': [{'interface': 'wan1', 
             'status': 'active', 
             'ip': 'x.x.x.x', 
             'gateway': 'x.x.x.x', 
             'publicIp': 'x.x.x.x', 
             'primaryDns': 'x.x.x.x', 
             'secondaryDns': 'x.x.x.x', 
             'ipAssignedBy': 'dhcp'}, 
             {'interface': 'wan2', 
             'status': 'ready', 
             'ip': 'x.x.x.x', 
             'gateway': 'x.x.x.x', 
             'publicIp': 'x.x.x.x', 
             'primaryDns': 'x.x.x.x', 
             'secondaryDns': 'x.x.x.x', 
             'ipAssignedBy': 'dhcp'}]}, 

{'networkId': 'B_0987654321', 
'serial': '1234-abcd-1234', 
'model': 'MXYZ', 
'lastReportedAt': '2021-01-01T00:00:00Z', 
'uplinks': [{'interface': 'wan1', 
             'status': 'active', 
             'ip': 'x.x.x.x', 
             'gateway': 'x.x.x.x', 
             'publicIp': 'x.x.x.x', 
             'primaryDns': 'x.x.x.x', 
             'secondaryDns': 'x.x.x.x', 
             'ipAssignedBy': 'dhcp'}, 
             {'interface': 'wan2', 
             'status': 'failed', 
             'ip': 'x.x.x.x', 
             'gateway': 'x.x.x.x', 
             'publicIp': 'x.x.x.x', 
             'primaryDns': 'x.x.x.x', 
             'secondaryDns': 'x.x.x.x', 
             'ipAssignedBy': 'dhcp'}]}, 

这些是 50 多个网络中的 2 个。

我想要它做的是:

找到“失败”状态,并打印连接错误,我在哪里可以找到它:

['networkId: XYZ', 'network name: ABC', 'interface: wan1', 'uplinks: failed'] 或类似的东西使其可读。

此外,我还获得了这段代码来从 networkId 创建网络名称并搜索上行链路,但我无法使其工作。

net_names = {
'A_1234567890':'Amsterdam', 
'B_0987654321':'Rotterdam'
}
network_id = response_json.get('networkId')
for item in response_json['uplinks']:
    if item['status'] == "active":
        print('network ID:', network_id,'network_name:',net_names.get(network_id), 'Interface:',item['interface'])

我对 Python 还是很陌生,我阅读了很多关于 JSON 的内容,但它们全都在字典和列表上,对我来说没有多大意义,以帮助我解决我的具体问题。

非常感谢任何帮助。

【问题讨论】:

    标签: python json meraki-api


    【解决方案1】:

    试试这个:

    json_data = response.json()
    
    net_names = {"A_1234567890": "Amsterdam", "B_0987654321": "Rotterdam"}
    for network_data in json_data:
        network_id = network_data.get("networkId")
        for uplink_data in network_data.get("uplinks", []):
            if uplink_data["status"] == "active":
                print(
                    "network ID:",
                    network_id,
                    "network_name:",
                    net_names.get(network_id, "n/a"),
                    "Interface:",
                    uplink_data["interface"],
                )
    

    【讨论】:

    • 是的!之后很少进行编辑以使其完美运行,但是是的,就是这样。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多