簡體   English   中英

在 Python 如何從 Json 響應中獲取特定字段?

[英]In Python How do I get specific Fields from Json response?

大家好,這里是我的 Json 回復:

{
  "assets": [
    {
      "id": 518447,
      "created_at": "2019-09-10T10:13:38Z",
      "priority": 10,
      "operating_system": "Microsoft - Windows - Windows Server 2008 R2, Enterprise Edition - SP1",
      "notes": null,
      "last_booted_at": null,
      "primary_locator": "external_id",
      "locator": "1112359",
      "vulnerabilities_count": 22,
      "status": "active",
      "last_seen_time": "2019-09-08T16:00:17Z",
      "network_ports": [
        {
          "id": 33550493,
          "port_number": 180,
          "extra_info": "",
          "hostname": null,
          "name": "HTTP",
          "ostype": "",
          "product": "JBoss EAP",
          "protocol": "tcp",
          "state": "open",
          "version": "4.2.3.GA"
        },
        {
          "id": 33550494,
          "port_number": 100,
          "extra_info": "",
          "hostname": null,
          "name": "SNMP",
          "ostype": "",
          "product": null,
          "protocol": "udp",
          "state": "open",
          "version": null
        },

      ],
      "tags": [
        "Windows Server",
        "DO - DO SPG BOM"
      ],
      "owner": null,
      "urls": {
        "vulnerabilities": ""
      },
      "ip_address": "10.10.10.1",
      "database": null,
      "hostname": null,
      "fqdn": null,
      "netbios": null,
      "application": null,
      "file": null,
      "mac_address": null,
      "ec2": null,
      "url": null,
      "external_id": "1112359",
      "ipv6": null,
      "asset_groups": [
        {
          "id": 4,
          "name": "0 Global - All"
        },
        {
          "id": 204,
          "name": "DO - All"
        },
        {
          "id": 417,
          "name": "Do - All"
        }
      ]
    },

我想要做的是獲取特定字段,例如標簽、操作系統等,但我不知道在這種情況下我應該實施什么邏輯來挑選我想要收集的信息。

這是我到目前為止所做的

import requests
import  gzip
import json

url = 'https://api.thisismyurl.com/assets/'
token = 'Blahblahblah'
# 'Content-Type': 'application/json'
headers = {'X-Risk-Token': token, 'Accept': 'application/json'}
response = requests.get(url,headers=headers)
print(response.status_code)
json_format = json.loads(response.text)

這是我不知道在這種情況下必須如何前進的地方。

有任何想法嗎?

首先,您的 json 無效是缺少右括號。 這個有效:

{
"assets": [{
            "id": 518447,
            "created_at": "2019-09-10T10:13:38Z",
            "priority": 10,
            "operating_system": "Microsoft - Windows - Windows Server 2008 R2, Enterprise Edition - SP1",
            "notes": null,
            "last_booted_at": null,
            "primary_locator": "external_id",
            "locator": "1112359",
            "vulnerabilities_count": 22,
            "status": "active",
            "last_seen_time": "2019-09-08T16:00:17Z",
            "network_ports": [{
                    "id": 33550493,
                    "port_number": 180,
                    "extra_info": "",
                    "hostname": null,
                    "name": "HTTP",
                    "ostype": "",
                    "product": "JBoss EAP",
                    "protocol": "tcp",
                    "state": "open",
                    "version": "4.2.3.GA"
                },
                {
                    "id": 33550494,
                    "port_number": 100,
                    "extra_info": "",
                    "hostname": null,
                    "name": "SNMP",
                    "ostype": "",
                    "product": null,
                    "protocol": "udp",
                    "state": "open",
                    "version": null
                }
            ],
            "tags": [
                "Windows Server",
                "DO - DO SPG BOM"
            ],
            "owner": null,
            "urls": {
                "vulnerabilities": ""
            },
            "ip_address": "10.10.10.1",
            "database": null,
            "hostname": null,
            "fqdn": null,
            "netbios": null,
            "application": null,
            "file": null,
            "mac_address": null,
            "ec2": null,
            "url": null,
            "external_id": "1112359",
            "ipv6": null,
            "asset_groups": [{
                    "id": 4,
                    "name": "0 Global - All"
                },
                {
                    "id": 204,
                    "name": "DO - All"
                },
                {
                    "id": 417,
                    "name": "Do - All"
                }
        ]
    }]
}

使用 json 創建變量后,您可以通過以下方式訪問數據:

print(json_format['assets'])

這將返回 ['assets'] 中的所有對象:

[{'id': 518447, 'created_at': '2019-09-10T10:13:38Z', 'priority': 10, 'operating_system': 'Microsoft - Windows - Windows Server 2008 R2, Enterprise Edition - SP1', 'notes': None, 'last_booted_at': None, 'primary_locator': 'external_id', 'locator': '1112359', 'vulnerabilities_count': 22, 'status': 'active', 'last_seen_time': '2019-09-08T16:00:17Z', 'network_ports': [{'id': 33550493, 'port_number': 180, 'extra_info': '', 'hostname': None, 'name': 'HTTP', 'ostype': '', 'product': 'JBoss EAP', 'protocol': 'tcp', 'state': 'open', 'version': '4.2.3.GA'}, {'id': 33550494, 'port_number': 100, 'extra_info': '', 'hostname': None, 'name': 'SNMP', 'ostype': '', 'product': None, 'protocol': 'udp', 'state': 'open', 'version': None}], 'tags': ['Windows Server', 'DO - DO SPG BOM'], 'owner': None, 'urls': {'vulnerabilities': ''}, 'ip_address': '10.10.10.1', 'database': None, 'hostname': None, 'fqdn': None, 'netbios': None, 'application': None, 'file': None, 'mac_address': None, 'ec2': None, 'url': None, 'external_id': '1112359', 'ipv6': None, 'asset_groups': [{'id': 4, 'name': '0 Global - All'}, {'id': 204, 'name': 'DO - All'}, {'id': 417, 'name': 'Do - All'}]}]

您可以在 ['assets'] object 之后直接添加 select 下一個 object:

print(json_format['assets'][0]['ip_address'])

返回:

10.10.10.1

這一直在繼續:

print(json_format['assets'][0]['network_ports'][0]['version'])

返回

4.2.3.GA

[0] 用於我們想要來自第一次出現的 ['assets'] object 和第一次出現的網絡端口的數據。 [1] 將是具有此標題的第二個 object。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM