python从json提取特定的键和值不起作用

我正在尝试使用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","CVE_data_meta" : {
        "ID" : "CVE-2020-0002","references" : {
        "reference_data" : [ {
          "url" : "https://source.android.com/security/bulletin/2020-01-04","name" : "https://source.android.com/security/bulletin/2020-01-04","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,and Android-10 Android ID: A-142602711"
        } ]
      }
    },...

我需要提取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,': ')):
                                                                                                          ^
SyntaxError: invalid syntax

我可以用以下命令打印出整个json:

print(json.dumps(cve_dict['CVE_Items'][0],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和说明,而不是所有键/值?

hushikai123 回答:python从json提取特定的键和值不起作用

当可以轻松遍历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'])
,

您能否尝试遍历字典:

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

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

本文链接:https://www.f2er.com/2800575.html

大家都在问