【问题标题】:how to extract field from JSON that return two string withing the same JSON如何从 JSON 中提取在同一 JSON 中返回两个字符串的字段
【发布时间】: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")
  • 除了主要问题:您的texthtml 似乎被翻转了,因为text 包含&lt;p&gt;...&lt;/p&gt; 结构,而html 仅包含“渲染”数据。
  • 这是从服务器端返回的 - 我认为它们也被翻转了

标签: java json string


【解决方案1】:

不是提取到字符串,而是将描述提取为 JSON 对象。
所以你的代码看起来是这样的,

JSONObject json3 = json2.getJSONObject("description")

那么,

String html = json3.get("html")
String text = json3.get("text")

另外,快速提醒一下,我使用的是org.json

Edit1:由于您使用的是 simple.json

JSONObject json2 = (JSONObject) object.get("description");
String html = (String) json2.get("html");
String text = (String) json2.get("text");

【讨论】:

  • JSONObject json2 = (JSONObject) object.get("description"); , 对象未知。
【解决方案2】:

最好的方法是使用 JsonPath click here 来了解更多工作示例。

【讨论】:

  • 我不想使用其他库!
猜你喜欢
  • 2016-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多