如何从JSON提取字段并返回两个具有相同JSON的字符串

时间:2019-06-21 12:38:10

标签: java json string

我有这样的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" );
    }

有没有一种方法只能提取文本内容?

3 个答案:

答案 0 :(得分:1)

将描述提取为JSON对象,而不是提取为String。
所以您的代码应该是这样,

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");

答案 1 :(得分:0)

最好的方法是使用JsonPath click here通过实例了解更多信息。

答案 2 :(得分:-1)

我对此进行了修正:

 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_descr = json2.get("description").toString();
                                    JSONObject json3 = (JSONObject) parser.parse(scan_vuln_descr);
                                    String description_as_TEXT = json3.get("text").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(description_as_TEXT);
                                    System.out.print("\n Rapid7 : Successful GET,  vulnerabilities info of : "+ scan_vuln_title + " were retrieved" );
                                }