我创建了一个 http GET 响应解析器,我可以在其中获取 json 数据的键和值并将其输入到 tableRow 对象中,但我收到 java.lang.ClassCastException
这是我的 URL 中的 JsonResponse:
{"vulnerability":{"id":15017916,"status":"open","closed_at":null,"created_at":"2019-07-26T10:06:03Z","due_date":null,"notes":null,"port":[],"priority":null,"identifiers":["adobe-flash-apsb14-21-cve-2014-0556"],"last_seen_time":"2019-07-24T06:00:00.000Z","fix_id":691,"scanner_vulnerabilities":[{"port":null,"external_unique_id":"adobe-flash-apsb14-21-cve-2014-0556","open":true}],"asset_id":291633,"connectors":[{"id":7,"name":"Nexpose Enterprise","connector_definition_name":"Nexpose Enterprise","vendor":"Rapid7"}],"service_ticket":null,"urls":{"asset":"api.nyc3.us.kennasecurity.com/assets/291633"},"patch":true,"patch_published_at":"2014-09-11T07:57:40.000Z","cve_id":"CVE-2014-0556","cve_description":"Heap-based buffer overflow in Adobe Flash Player before 13.0.0.244 and 14.x and 15.x before 15.0.0.152 on Windows and OS X and before 11.2.202.406 on Linux, Adobe AIR before 15.0.0.249 on Windows and OS X and before 15.0.0.252 on Android, Adobe AIR SDK before 15.0.0.249, and Adobe AIR SDK & Compiler before 15.0.0.249 allows attackers to execute arbitrary code via unspecified vectors, a different vulnerability than CVE-2014-0559.","cve_published_at":"2014-09-10T01:55:00.000Z","description":null,"solution":null,"wasc_id":null,"severity":10,"threat":10,"popular_target":false,"active_internet_breach":true,"easily_exploitable":true,"malware_exploitable":true,"predicted_exploitable":false,"custom_fields":[],"first_found_on":"2019-07-24T06:27:26Z","top_priority":true,"risk_meter_score":100,"closed":false}}
这是我的解析器方法:
public static TableRow parseRequest(String request, TableRow row) {
JsonParser jsonParser = new JsonParser();
//throws an exception here JsonArray jsonArray = (JsonArray) jsonParser.parse(request);
// this object is used to get the keys
com.google.gson.JsonObject firstJsonObject = jsonArray.get(0).getAsJsonObject();
java.util.Set<Entry<String,JsonElement>> entrySet = firstJsonObject.entrySet();
// declare two dimensional array
Object[][] array = new Object[entrySet.size()][jsonArray.size() + 1];
// the first column of the two-dimensional array is populated
Iterator<java.util.Map.Entry<String, JsonElement>> itr = entrySet.iterator();
for (int i = 0; itr.hasNext(); i++) {
array[i][0] = itr.next().getKey();
}
// the rest of the columns are populated
for (int i = 0; i < jsonArray.size(); i++) {
com.google.gson.JsonObject obj = (com.google.gson.JsonObject) jsonArray.get(i);
for (int j = 0; j < array.length; j++) {
String key = array[j][0].toString();
JsonElement value = (JsonElement) obj.get(key);
array[j][i + 1] = value instanceof JsonNull ? null : value.getAsString();
}
}
return row;
我不知道如何继续前进。有什么建议可以纠正这个错误吗?
最佳答案
您正在尝试将 JsonParser 转换为 JsonArray。这是不可能的,因为 JsonArray 不是子类(它不扩展也不实现 JsonParser)。您可能想要做的是将 JsonParser 转换为一个对象,如下所示。之后,您可以通过从 JSON 中识别关键字来获取数组。
JsonParser jsonParser = new JsonParser();
JSONObject jsonObject = (JSONObject) parser.parse(request);
//Obtain the array from the keyword in your JSON.
JSONArray zoneArray = (JSONArray) jsonObject.get("KeywordForTheArray");
祝你编码愉快!
关于Java:Json 响应类广播异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57456178/