gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:59:02 24 4
gpt4 key购买 nike

我能够使用我的 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 很陌生,所以我想知道是否有比我正在实现的方法更好的方法?

最佳答案

我建议为此使用 jackson 库。 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);
}

关于java - 如何解析获取请求响应以获取所有键和值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57383992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com