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

问题描述 投票:0回答:1

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

我想使用dumps命令,但是找不到一个很好的例子。

从json文件中提取出来的是(但是文件很长):

"cve": {
    "CVE_data_meta": {
        "ASSIGNER": "[email protected]",
        "ID": "CVE-2020-1785"
    },
    "data_format": "MITRE",
    "data_type": "CVE",
    "data_version": "4.0",
    "description": {
        "description_data": [
            {
                "lang": "en",
                "value": "Mate 10 Pro;Honor V10;Honor 10;Nova 4 smartphones have a denial of service vulnerability. The system does not properly check the status of certain module during certain operations, an attacker should trick the user into installing a malicious application, successful exploit could cause reboot of the smartphone."
            }
        ]
    },
    "problemtype": {
        "problemtype_data": [
            {
                "description": [
                    {
                        "lang": "en",
                        "value": "CWE-20"
                    }
                ]
            }
        ]
    },
    "references": {
        "reference_data": [
            {
                "name": "https://www.huawei.com/en/psirt/security-advisories/huawei-sa-20200102-03-smartphone-en",
                "refsource": "MISC",
                "tags": [
                    "Vendor Advisory"
                ],
                "url": "https://www.huawei.com/en/psirt/security-advisories/huawei-sa-20200102-03-smartphone-en"
            }
        ]
    }
},
...

我需要提取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,但不确定。

我正在尝试使用Python在Windows中从json提取特定的键和值。我想使用dumps命令,但是找不到一个很好的例子。 json文件的摘录是(但文件是...

json python-3.x key-value dump
1个回答
0
投票

您能否尝试遍历字典:

© www.soinside.com 2019 - 2024. All rights reserved.