[英]How to parse a jsonArray
我想解析一个 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 数组并获取列表中 ID 的值。 如果是 JSON 响应,我的代码将是
id = response.getBody.jsonPath.getList("vulnerabilities.Id");
但它是一个 JSON 文件。 我必须读取文件,然后解析 JSON 以将 id 的值提取到List
中。 有人可以帮忙吗?
(假设你是用 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());
然后从您的 object 中提取所需的字段。
//Or loop on each-element based on your use case
responses.get(0).getVulnerabilities().get(0).getId()
要么
使用 JsonArray/JsonObject class
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.