【发布时间】:2019-06-21 12:38:10
【问题描述】:
我的 JSON 看起来像这样
{
"description":
{
"html": "A remote code execution vulnerability exists in the way that the scripting engine handles objects in memory in Microsoft Edge. ...",
"text": "<p>A remote code execution vulnerability exists in the way that the scripting engine handles objects in memory in Microsoft Edge. ...</p>"
}
}
我提取了字段描述,但它同时包含 html 和文本,而我只对文本字段感兴趣。
while (true)
{
//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String lines = null;
StringBuilder stringBuilder = new StringBuilder();
while ((lines = bufferedReader.readLine()) != null)
{
stringBuilder.append(lines);
}
bufferedReader.close();
result = stringBuilder.toString();
JSONParser parser = new JSONParser();
JSONObject json2 = (JSONObject) parser.parse(result);
if(methodType == MethodType.Retrieve_Vulnerability_info)
{
String scan_vuln_title= json2.get("title").toString();
String scan_vuln_severityScore = json2.get("severityScore").toString();
String scan_vuln_publishe_date = json2.get("published").toString();
String scan_vuln_description = json2.get("description").toString();
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setSeverityScore(scan_vuln_severityScore);
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setVulnerability_title(scan_vuln_title);
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setPublished_date(scan_vuln_publishe_date);
splunkdata.getScan().getList_of_found_Vulnerabilties().get(Vulnerability_id).setDescription(scan_vuln_description);
System.out.print("\n Rapid7 : Successful GET, vulnerabilities info of : "+ scan_vuln_title + " were retrieved" );
}
有没有办法只提取文本内容?
【问题讨论】:
-
好吧,因为它是嵌套的 json,所以像
json2.get("description").get("text")这样的东西应该可以解决问题(假设json2表示地图、JsonObject 等) - 您可能需要添加一些解析和一些空检查,但是我会把它留给你。 -
json2的类型是什么?有许多不同的 JSON 解析器/库提供不同的 API,因此答案将取决于您使用的内容。 -
我正在使用 SimpleJSON 库,我已经添加了完整的代码,我没有 mond get("text")
-
除了主要问题:您的
text和html似乎被翻转了,因为text包含<p>...</p>结构,而html仅包含“渲染”数据。 -
这是从服务器端返回的 - 我认为它们也被翻转了