简体   繁体   中英

How to Parse Get Request Response to get All key and Value pairs?

I was able to pull data from an api using my get request method

 {"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}

the problem I have encountered is to parse this json data by separating it by colon and comma?

I have created a parser method show below:

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

The results that I get is java.lang.ClassCastException. I am very new to Json so I would like to know if there is a better method than the one I am implementing?

I would advise to use jackson library for this. Jackson is a library for parsing json string to java class. You can also generate a json from a java class. See here for more info and how to use it: https://www.baeldung.com/jackson-object-mapper-tutorial

Here is an example how you can set it up. First create a java pojo which should be equal to your response 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
}

And here is your parsing logic:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM