首页 > 解决方案 > 如何在python中访问列表列表

问题描述

下面是列表的样子。我想访问列表列表。我想访问它,因为我想将它存储到数据库中。response_body 看起来像这样。

[
  {
    "username_count": 0,
    "description": "som string",
    "rules": [
      {
        "id": 10845,
        "type": "some string"
      }
    ],
    "event_count": 7,
    "flow_count": 0,
    "assigned_to": null,
    "security_category_count": 3,
    "follow_up": false,
    "source_address_ids": [
      858,345
    ],
    "source_count": 1,
    "inactive": false,
    "protected": false,
    "category_count": 3,
    "source_network": "some string",
    "destination_networks": [
      "other",
      "some string"
    ],
    "closing_user": null,
    "close_time": null,
    "remote_destination_count": 1,
    "start_time": 1563267761163,
    "last_updated_time": 1563268006582,
    "credibility": 3,
    "magnitude": 6,
    "id": 4786,
    "categories": [
      "string1",
      "string2",
      "string3"
    ],
    "severity": 8,
    "policy_category_count": 0,
    "device_count": 3,
    "closing_reason_id": null,
    "offense_type": 0,
    "relevance": 5,
    "domain_id": 0,
    "offense_source": "172.168.15.120",
    "local_destination_address_ids": [
      3
    ],
    "local_destination_count": 1,
    "status": "OPEN"
  },

我试过这样做

 response_body = json.loads(response.read().decode('utf-8'))


    for j in response_body:
        obj, created = Offenses.objects.get_or_create(oid=j['id'], defaults={'description': j['description'], 'assigned_to': j['assigned_to'], 'categories':j['categories']})

但这不起作用,因为它返回 None

我应该如何访问上面列表中的类别,因为类别本身就是一个列表?它应该返回一些字符串,但返回 None。

标签: djangopython-3.x

解决方案


推荐阅读