【问题标题】:Python Filter JSON array dataPython过滤JSON数组数据
【发布时间】:2021-10-30 09:02:53
【问题描述】:

我正在尝试从 JSON 响应中提取所需的信息。在 computer_network_adapter_summary 下,有来自某些响应的多个适配器,有 5 个,在某些响应中 >12。我只对以太网(其中名称包含“Intel(R) Ethernet”)wifi(其中名称包含“Intel(R) Dual Band Wireless”)感兴趣p>

有什么方法可以只过滤所需的两个适配器的 MAC 地址而不是所有适配器?提前致谢。

response ='{"message_response":{"compdetailssummary":{"computer_summary":{"computer_name":"ABC","primary_owner":"Windows User","domain_name":"WORKGROUP"},"computer_network_adapter_summary":[{"dhcp_status":"Enabled","adapter_name":"[00000001] Fortinet Virtual Ethernet Adapter (NDIS 6.30)","mac_address":"00:00:0F:00:00:01","dhcp_server":"-","ip_address":"-","gateway":"-"},{"dhcp_status":"Enabled","adapter_name":"[00000004] Intel(R) Ethernet Connection I219-LM","mac_address":"00:00:0F:00:00:01","dhcp_server":"-","ip_address":"-","gateway":"-"},{"dhcp_status":"Enabled","adapter_name":"[00000005] Intel(R) Dual Band Wireless-AC 8260","mac_address":"00:00:0F:00:00:01","dhcp_server":"1.1.9.1","ip_address":"1.2.9.3","gateway":"1.1.8.1"},{"dhcp_status":"Enabled","adapter_name":"[00000007] Bluetooth Device (Personal Area Network)","mac_address":"00:00:0F:00:00:01","dhcp_server":"-","ip_address":"-","gateway":"-"},{"dhcp_status":"Disabled","adapter_name":"[00000002] PPPoP WAN Adapter","mac_address":"-","dhcp_server":"--","ip_address":"--","gateway":"--"},{"dhcp_status":"Enabled","adapter_name":"[00000003] Fortinet SSL VPN Virtual Ethernet Adapter","mac_address":"00:00:0F:00:00:01","dhcp_server":"--","ip_address":"--","gateway":"--"}]}}}'
json_data = json.loads(response)

computername = json_data['message_response']['compdetailssummary']['computer_summary']['computer_name']
ethMacAdd = json_data['message_response']['compdetailssummary']['computer_network_adapter_summary']
wifiMacAdd = json_data['message_response']['compdetailssummary']['computer_network_adapter_summary']

for item in json_data['message_response']['compdetailssummary']['computer_network_adapter_summary']:
    if item["adapter_name"] = "Intel(R) Dual Band Wireless":
        print(json_data['message_response']['compdetailssummary']['computer_network_adapter_summary'][0]["adapter_name"])

【问题讨论】:

  • 当然有办法。你试过什么?截至目前,您的问题类似于“为我做这个”,这里是off-topic。请拨打tour,阅读How to Askquestion checklist,并提供minimal reproducible example,询问关于您的尝试的具体问题
  • 亲爱的@PranavHosangadi 我不是要求为我做这件事。我搜索了它很困惑,这是我发布的。如果有任何可用的示例,请分享我可以理解的方法,我将自己做。到目前为止,我尝试了计算机名并尝试了以太网 mac,但它不起作用双频无线": print(d['message_response']['compdetailssummary']['computer_network_adapter_summary'][0]["adapter_name"])

标签: python arrays json


【解决方案1】:

类似的东西

data = {
    "message_response": {
        "compdetailssummary": {
            "computer_summary": {
                "computer_name": "ABC",
                "primary_owner": "Windows User",
                "domain_name": "WORKGROUP"
            },
            "computer_network_adapter_summary": [
                {
                    "dhcp_status": "Enabled",
                    "adapter_name": "[00000001] Fortinet Virtual Ethernet Adapter (NDIS 6.30)",
                    "mac_address": "00:00:0F:00:00:01",
                    "dhcp_server": "-",
                    "ip_address": "-",
                    "gateway": "-"
                },
                {
                    "dhcp_status": "Enabled",
                    "adapter_name": "[00000004] Intel(R) Ethernet Connection I219-LM",
                    "mac_address": "00:00:0F:00:00:01",
                    "dhcp_server": "-",
                    "ip_address": "-",
                    "gateway": "-"
                },
                {
                    "dhcp_status": "Enabled",
                    "adapter_name": "[00000005] Intel(R) Dual Band Wireless-AC 8260",
                    "mac_address": "00:00:0F:00:00:01",
                    "dhcp_server": "1.1.9.1",
                    "ip_address": "1.2.9.3",
                    "gateway": "1.1.8.1"
                },
                {
                    "dhcp_status": "Enabled",
                    "adapter_name": "[00000007] Bluetooth Device (Personal Area Network)",
                    "mac_address": "00:00:0F:00:00:01",
                    "dhcp_server": "-",
                    "ip_address": "-",
                    "gateway": "-"
                },
                {
                    "dhcp_status": "Disabled",
                    "adapter_name": "[00000002] PPPoP WAN Adapter",
                    "mac_address": "-",
                    "dhcp_server": "--",
                    "ip_address": "--",
                    "gateway": "--"
                },
                {
                    "dhcp_status": "Enabled",
                    "adapter_name": "[00000003] Fortinet SSL VPN Virtual Ethernet Adapter",
                    "mac_address": "00:00:0F:00:00:01",
                    "dhcp_server": "--",
                    "ip_address": "--",
                    "gateway": "--"
                }
            ]
        }
    }
}

interesting_adapters = [adapter for adapter in data['message_response']['compdetailssummary']['computer_network_adapter_summary'] if
                        'Intel(R) Ethernet' in adapter['adapter_name'] or 'Intel(R) Dual Band Wireless' in adapter[
                            'adapter_name']]

print(interesting_adapters)
print(len(interesting_adapters))

输出

[{'dhcp_status': 'Enabled', 'adapter_name': '[00000004] Intel(R) Ethernet Connection I219-LM', 'mac_address': '00:00:0F:00:00:01', 'dhcp_server': '-', 'ip_address': '-', 'gateway': '-'}, {'dhcp_status': 'Enabled', 'adapter_name': '[00000005] Intel(R) Dual Band Wireless-AC 8260', 'mac_address': '00:00:0F:00:00:01', 'dhcp_server': '1.1.9.1', 'ip_address': '1.2.9.3', 'gateway': '1.1.8.1'}]
2

【讨论】:

  • 亲爱的@balderman 非常感谢。非常感谢您的支持。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2017-08-18
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多