gson 如何解析jsonArray

4urapxun  于 2023-01-05  发布在  其他
关注(0)|答案(1)|浏览(218)

我想解析一个JSON数组,下面是JSON示例

[
    {"Vulnerabilities": [
            {
                "Id": "Cx35ef42d7-054c",
                "CveName": "",
                "Score": 9.8,
                "Severity": "High",
                "PublishDate": "2021-01-22T13:34:00",
                "References": [
                    "https://github.com/mde/ejs/issues/571",
                    "https://github.com/mde/ejs/commit/abaee2be937236b1b8da9a1f55096c17dda905fd"
                ],
                "Description": "ejs package before 3.1.6 is vulnerable to arbitrary code injection. The vulnerability exists due to improper input validation passed via the options parameter - the filename, compileDebug, and client option.",
                "Cvss": {
                    "Score": 9.8,
                    "Severity": "High",
                    "AttackVector": "NETWORK",
                    "AttackComplexity": "LOW",
                    "Confidentiality": "HIGH",
                    "Availability": "HIGH",
                    "ExploitCodeMaturity": null,
                    "RemediationLevel": null,
                    "ReportConfidence": null,
                    "ConfidentialityRequirement": null,
                    "IntegrityRequirement": null,
                    "AvailabilityRequirement": null,
                    "Version": 3.0
                },
                "Recommendations": null,
                "PackageId": "Npm-ejs-2.7.4",
                "FixResolutionText": "3.1.7",
                "IsIgnored": true,
                "ExploitableMethods": [],
                "Cwe": "CWE-94",
                "IsViolatingPolicy": true,
                "IsNewInRiskReport": false,
                "Type": "Regular"
            },
            {
                "Id": "CVE-2022-29078",
                "CveName": "CVE-2022-29078",
                "Score": 9.8,
                "Severity": "High",
                "PublishDate": "2022-04-25T15:15:00",
                "References": [
                    "https://github.com/advisories/GHSA-phwq-j96m-2c2q",
                    "https://eslam.io/posts/ejs-server-side-template-injection-rce/",
                    "https://github.com/mde/ejs/commit/61b6616fd34ff4d21c38fe1dbaf2b3aa936bb749",
                    "https://github.com/mde/ejs/issues/451",
                    "https://github.com/mde/ejs/pull/601"
                ],
                "Description": "The ejs (aka Embedded JavaScript templates) package up to 3.1.6 for Node.js allows server-side template injection in settings[view options][outputFunctionName]. This is parsed as an internal option, and overwrites the outputFunctionName option with an arbitrary OS command (which is executed upon template compilation).",
                "Cvss": {
                    "Score": 9.8,
                    "Severity": "High",
                    "AttackVector": "NETWORK",
                    "AttackComplexity": "LOW",
                    "Confidentiality": "HIGH",
                    "Availability": "HIGH",
                    "ExploitCodeMaturity": null,
                    "RemediationLevel": null,
                    "ReportConfidence": null,
                    "ConfidentialityRequirement": null,
                    "IntegrityRequirement": null,
                    "AvailabilityRequirement": null,
                    "Version": 3.0
                },
                "Recommendations": null,
                "PackageId": "Npm-ejs-2.7.4",
                "FixResolutionText": "3.1.7",
                "IsIgnored": true,
                "ExploitableMethods": [],
                "Cwe": "CWE-74",
                "IsViolatingPolicy": true,
                "IsNewInRiskReport": false,
                "Type": "Regular"
            }
}
]

我想解析JSON数组并在一个列表中获取Ids的值。

id = response.getBody.jsonPath.getList("vulnerabilities.Id");

但这是一个JSON文件。我必须读取该文件,然后解析JSON以将id的值提取到List中。有人能帮忙吗?

c2e8gylq

c2e8gylq1#

(假设您使用Java)
您可以执行以下操作(使用Google gson):

Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader(file_path));

在此之后-有两种方法,
或者,
创建与对象匹配的POJO**(首选)**

ResponseHolder response = gson.fromJson(reader, ResponseHolder.class);

在您的示例中,因为它是一个数组

List<ResponseHolder> responses = gson.fromJson(yourJson, new TypeToken<List<ResponseHolder>>() {}.getType());

然后从对象中提取所需字段。

//Or loop on each-element based on your use case
responses.get(0).getVulnerabilities().get(0).getId()


使用JsonArray/JsonObject类

JsonArray responses = gson.fromJson(reader, JsonArray.class);
for (JsonElement response : responses) {
     JsonObject item = response.getAsJsonObject();
     JsonArray vulnerabilities = item.get("vulnerabilities").getAsJsonArray();
     //Or Loop
     Strring idOfFirst = vulnerabilities.get(0).getAsJsonObject("id").getAsString();
}

相关问题