如何解析获取请求响应以获取所有键和值对?

时间:2019-08-06 21:05:59

标签: java json api httprequest classcastexception

我能够使用get请求方法从api中提取数据

 {"vulnerabilities":[{"id":5027994,"status":"open","closed_at":null,"created_at":"2019-06-07T06:10:15Z","due_date":null,"notes":null,"port":[],"priority":null,"identifiers":["adobe-reader-apsb09-15-cve-2009-2990"],"last_seen_time":"2019-07-24T05:00:00.000Z","fix_id":4953,"scanner_vulnerabilities":[{"port":null,"external_unique_id":"adobe-reader-apsb09-15-cve-2009-2990","open":true}],"asset_id":119920,"connectors":[{"name":"Nexpose Enterprise","id":7,"connector_definition_name":"Nexpose Enterprise","vendor":"R7"}],"service_ticket":null,"urls":{"asset":"dummy.com"},"patch":true,"patch_published_at":"2009-10-08T22:40:52.000Z","cve_id":"CVE-2009-2990","cve_description":"Array index error in Adobe Reader and Acrobat 9.x before 9.2, 8.x before 8.1.7, and possibly 7.x through 7.1.4 might allow attackers to execute arbitrary code via unspecified vectors.","cve_published_at":"2009-10-19T22:30:00.000Z","description":null,"solution":null,"wasc_id":null,"severity":9,"threat":9,"popular_target":false,"active_internet_breach":true,"easily_exploitable":true,"malware_exploitable":true,"predicted_exploitable":false,"custom_fields":[],"first_found_on":"2019-06-05T05:22:23Z","top_priority":true,"risk_meter_score":100,"closed":false}

我遇到的问题是通过用冒号和逗号分隔来解析此json数据?

我创建了一个解析器方法,如下所示:

public static TableRow parseRequest(String request, TableRow row) {
        JsonParser parser= new JsonParser();
        try {
            Object object = parser.parse(request);
//throws an ClassCastException  JsonObject jsonObject = (JsonObject) object;
            JsonArray array = (JsonArray) object;

            for (Iterator iterator = jsonObject.keySet().iterator(); iterator.hasNext(); ) {
                String keyString = (String) iterator.next();

                System.out.println("iterator" + iterator);
                System.out.println(jsonObject.get(keyString));
            }

        } catch (Exception e) {

            e.printStackTrace();
            // TODO: handle exception
        }
        return parserequesTableRow(row);
}

我得到的结果是java.lang.ClassCastException。我对Json还是很陌生,所以我想知道是否有比我正在实施的方法更好的方法?

1 个答案:

答案 0 :(得分:0)

我建议为此使用杰克逊图书馆。 Jackson是一个用于将json字符串解析为java类的库。您还可以从Java类生成json。有关更多信息及其使用方法,请参见此处:https://www.baeldung.com/jackson-object-mapper-tutorial

这里是如何设置它的示例。首先创建一个Java pojo,它应该等于您的响应json:

@JsonInclude(NON_NULL)
class Response {

    private List<Vulnerability> vulnerabilities;

    // getters and setters
}

@JsonInclude(NON_NULL)
class Vulnerability {

    private String id;
    private String status;
    private String closed_at;
    private String created_at;
    private String due_date;
    private String notes;
    private String[] port;
    private String priority;
    // etc for other class members

    // getters and setters
}

这是您的解析逻辑:

public static void main(String[] args) throws IOException {
    String json = ""; //put here your data which you got from your get request
    ObjectMapper objectMapper = new ObjectMapper();
    Response response = objectMapper.readValue(json, Response.class);
}