在Python中,如何从Json响应中获取特定字段?

时间:2019-11-04 20:10:39

标签: python json api nested

大家好,我的杰森回答:

 child: ListTile(
    leading: FlutterLogo(size: 72.0),
    title: Text('Three-line ListTile'),
    subtitle: Text(
      'A sufficiently long subtitle warrants three lines.'
    ),
    trailing: Icon(Icons.more_vert),
    isThreeLine: true,
  ),

我想做的是获取特定的字段,例如标签,操作系统等,但是我不知道在这种情况下应该实现什么逻辑来挑选我想要收集的信息。 / p>

这是我到目前为止所做的

{
  "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"
        }
      ]
    },

在这种情况下,我不知道该如何前进。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

首先,您的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']对象之后直接添加下一个对象来选择下一个对象:

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']对象的第一次出现到网络端口的第一次出现的数据。 [1]将是具有此标题的第二个对象。