【问题标题】:python extracting specific key and value from json not workingpython从json中提取特定的键和值不起作用
【发布时间】:2020-01-09 02:15:21
【问题描述】:

我正在尝试使用 Python 从 Windows 中的 json 中提取特定的键和值。

我想使用 dumps 命令,但找不到好的示例。

**更新数据:json文件的摘录是(但文件很长):

   {
  "CVE_data_type" : "CVE",
  "CVE_data_format" : "MITRE",
  "CVE_data_version" : "4.0",
  "CVE_data_numberOfCVEs" : "64",
  "CVE_data_timestamp" : "2020-01-09T08:00Z",
  "CVE_Items" : [ {
    "cve" : {
      "data_type" : "CVE",
      "data_format" : "MITRE",
      "data_version" : "4.0",
      "CVE_data_meta" : {
        "ID" : "CVE-2020-0001",
        "ASSIGNER" : "cve@mitre.org"
      },
      "problemtype" : {
        "problemtype_data" : [ {
          "description" : [ ]
        } ]
      },
      "references" : {
        "reference_data" : [ {
          "url" : "https://source.android.com/security/bulletin/2020-01-01",
          "name" : "https://source.android.com/security/bulletin/2020-01-01",
          "refsource" : "CONFIRM",
          "tags" : [ ]
        } ]
      },
      "description" : {
        "description_data" : [ {
          "lang" : "en",
          "value" : "In getProcessRecordLocked of ActivityManagerService.java isolated apps are not handled correctly. This could lead to local escalation of privilege with no additional execution privileges needed. User interaction is not needed for exploitation. Product: Android Versions: Android-8.0, Android-8.1, Android-9, and Android-10 Android ID: A-140055304"
        } ]
      }
    },
    "configurations" : {
      "CVE_data_version" : "4.0",
      "nodes" : [ ]
    },
    "impact" : { },
    "publishedDate" : "2020-01-08T19:15Z",
    "lastModifiedDate" : "2020-01-08T20:01Z"
  }, {
    "cve" : {
      "data_type" : "CVE",
      "data_format" : "MITRE",
      "data_version" : "4.0",
      "CVE_data_meta" : {
        "ID" : "CVE-2020-0002",
        "ASSIGNER" : "cve@mitre.org"
      },
      "problemtype" : {
        "problemtype_data" : [ {
          "description" : [ ]
        } ]
      },
      "references" : {
        "reference_data" : [ {
          "url" : "https://source.android.com/security/bulletin/2020-01-04",
          "name" : "https://source.android.com/security/bulletin/2020-01-04",
          "refsource" : "CONFIRM",
          "tags" : [ ]
        } ]
      },
      "description" : {
        "description_data" : [ {
          "lang" : "en",
          "value" : "In ih264d_init_decoder of ih264d_api.c, there is a possible out of bounds write due to a use after free. This could lead to remote code execution with no additional execution privileges needed. User interaction is needed for exploitation Product: Android Versions: Android-8.0, Android-8.1, Android-9, and Android-10 Android ID: A-142602711"
        } ]
      }
    },
    "configurations" : {
      "CVE_data_version" : "4.0",
      "nodes" : [ ]
    },

    ...

我需要提取 ID 和描述。

我试过了

for key, value in json.dumps(cve_dict['CVE_Items'][0], sort_keys=True, indent=4, separators=',', ': ')):
    #if(key in ['ID', 'description']):
    print(key, value)

但我收到此错误:

  File "unzip_get_info.py", line 19
    for key, value in json.dumps(cve_dict['CVE_Items'][0], sort_keys=True, indent=4, separators=',', ': ')):
                                                                                                          ^
SyntaxError: invalid syntax

我可以用这个打印整个 json:

print(json.dumps(cve_dict['CVE_Items'][0], sort_keys=True, indent = 4, separators=(',', ': ')))

我不是一个庞大的 Python 程序员。知道如何获取 ID 和描述的键/值吗?我尝试使用 cve_dict 直接执行此操作,但错误在于我无法直接执行此操作。

非常感谢您的帮助。这是我的第一个 python 程序。

我试图弄清楚如何使用这个link 处理转储,但我没有看到它。我看了this too,但我不确定。

**更新:我试过了

for key, value in cve_dict['CVE_Items'][0].items():
    if(key in ['ID', 'description']):
        print(key, value)

除了下面建议的内容外,还试图将其限制为 ID 和描述,但它没有打印任何内容。我知道 ID 和描述在那里(见上面的 json)。如何只打印 ID 和描述,而不是所有键/值?

【问题讨论】:

    标签: json python-3.x key-value dump


    【解决方案1】:

    当 dict 本身可以轻松遍历时,您不应该将 dict 转换为 JSON 字符串。只需使用以下命令访问 ID 键的所需值:

    cve_dict['CVE_Items'][0]['cve']['CVE_data_meta']['ID']
    

    如果您想要所有 ID,您可以使用 for 循环遍历列表项:

    for item in cve_dict['CVE_Items']:
        print(item['cve']['CVE_data_meta']['ID'])
    

    【讨论】:

    • 非常感谢!我以为我明白了,但是当我尝试使用 print(cve_dict['CVE_Items'][0]['cve']['description']['description_data']['value'] ) 获取描述时,它说 TypeError: list索引必须是整数或切片,而不是 str
    • 不客气。 descriptiondescription_data 的值都是列表,因此您应该通过索引访问子字典:cve_dict['CVE_Items'][0]['cve']['description'][0]['description_data'][0]['value']
    • Idk,它说 KeyError: 0 with print(cve_dict['CVE_Items'][0]['cve']['description'][0]['description_data'][0]['值'])
    • 哎呀我以为description的值是一个列表,其实不是,所以:cve_dict['CVE_Items'][0]['cve']['description']['description_data'][0]['value']
    • 您能否使用具有多个cve ID 的数据样本更新您的问题,以便清楚多个 ID 的位置?
    【解决方案2】:

    你可以尝试迭代 dict:

    for k, v in cve_dict['CVE_Items'][0].items():
        print(k, v)
    

    json.dumps 是将 dict 转换回字符串,而不是要迭代的 python 对象,

    【讨论】:

    • 它正在打印信息,但如何将其限制为 ID 和描述的 KEY/值?
    • 我在问题中添加了**更新。当我按照你说的做时,它会打印所有内容,而不仅仅是 ID 和描述。当我尝试添加 if 时,它什么也不打印。
    猜你喜欢
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2019-05-23
    • 2023-01-16
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多