[英]Java - How to Parse empty Nested Json Array
我正在尝试使用(GSON)库将以下 JSON 数据解析为 Java 中的字符串,我能够解析除 JSON 数组之一之外的所有 JSON 字段数据。 我想检查它是否为空/空然后在字符串变量中存储空值,如果不是则存储原始值。
输入 JSON 数据:
{
"expand": "schema,names",
"startAt": 0,
"maxResults": 50,
"total": 37875,
"issues": [
{
"id": "1190",
"key": "GDS-81",
"fields": {
"issuetype": {
"id": "2170",
"name": "Service Request with Approvals",
"subtask": false
},
"customfield_29805": {
"id": "26",
"name": "Issue - First Response",
"completedCycles": []
}
}
}
]
}
我到目前为止所做的代码,
JsonObject object = (JsonObject) new JsonParser().parse(jsonResponse);
JsonArray issuesArray = object.getAsJsonArray("issues");
for(int i=0; i<issuesArray.size(); i++) {
JsonObject currentissues = (JsonObject) issuesArray.get(i);
String Issue_Id = (String) currentissues.get("id").toString().replace("\"", "");
String Issue_Key = (String) currentissues.get("key").toString().replace("\"", "");
String Issue_Type = (String) currentissues.get("fields").getAsJsonObject().get("issuetype").getAsJsonObject().get("name").getAsString();
JsonObject customfield = (JsonObject) currentissues.get("fields").getAsJsonObject().get("customfield_29805");
JsonArray completedCyclesArray= customfield.getAsJsonArray("completedCycles");
String Issue_FirstResponseStartTime = (completedCyclesArray.size() > 0) ? completedCyclesArray.getAsString() : "NULL";
}
但是,当我执行代码时,我在线上出现以下错误: JsonObject customfield
java.lang.ClassCastException: com.google.gson.JsonNull cannot be cast to com.google.gson.JsonObject
[![UpdatedCode StackTrace][1]][1] [1]: https://i.stack.imgur.com/2wY0S.jpg
您不需要显式地将 JsonElement 转换为 JsonObject 而是使用 getAsJsonArray ,一旦获得数组,就可以遍历它的所有元素。
在检查它的大小之前,您还需要处理对 CompletedCyclesArray 的空检查,否则它会给您 NPE ,我也修复了它。 请找到修改后的工作代码如下
JsonParser parser = new JsonParser(); JsonArray array = parser.parse(jsonResponse).getAsJsonArray(); for(JsonElement e : array) { JsonObject currentissues = (JsonObject) e; String Issue_Id = (String) currentissues.get("id").toString().replace("\\"", ""); String Issue_Key = (String) currentissues.get("key").toString().replace("\\"", ""); String Issue_Type = (String) currentissues.get("fields").getAsJsonObject().get("issuetype").getAsJsonObject().get("name").getAsString(); JsonObject customfield = (JsonObject) currentissues.get("fields").getAsJsonObject().get("customfield_29805"); JsonArray completedCyclesArray= customfield.getAsJsonArray("completedCycles"); String Issue_FirstResponseStartTime = (null != completedCyclesArray && completedCyclesArray.size() > 0) ? completedCyclesArray.getAsString() : "NULL"; }
}
请为更新的 json 请求(不是数组而是嵌套的 json 请求)找到我的工作解决方案
JsonObject object = (JsonObject) new JsonParser().parse(jsonResponse);
JsonArray issuesArray = object.getAsJsonArray("issues");
String expand = object.get("expand").toString();
String startAt = object.get("startAt").toString();
String maxResults = object.get("maxResults").toString();
String total = object.get("total").toString();
System.out.println(String.format("expand %s , startAt %s, maxResults %s, total %s", expand, startAt, maxResults, total));
IntStream.range(0, issuesArray.size()).mapToObj(i -> (JsonObject) issuesArray.get(i)).forEach(currentissues -> {
String Issue_Id = (String) currentissues.get("id").toString().replace("\"", "");
String Issue_Key = (String) currentissues.get("key").toString().replace("\"", "");
String Issue_Type = (String) currentissues.get("fields").getAsJsonObject().get("issuetype").getAsJsonObject().get("name").getAsString();
JsonObject customfield = (JsonObject) currentissues.get("fields").getAsJsonObject().get("customfield_29805");
JsonArray completedCyclesArray = customfield.getAsJsonArray("completedCycles");
String Issue_FirstResponseStartTime = (completedCyclesArray.size() > 0) ? completedCyclesArray.toString() : "NULL";
System.out.println(String.format("Issue_Id %s , Issue_Key %s, Issue_Type %s, Issue_FirstResponseStartTime %s", Issue_Id, Issue_Key, Issue_Type, Issue_FirstResponseStartTime));
});
这是我得到的输出:
展开 "schema,names" , startAt 0, maxResults 50, total 37875 Issue_Id 1190 , Issue_Key GDS-81, Issue_Type Service Request with Approvals, Issue_FirstResponseStartTime NULL
请在此处查看我的完整工作代码完整代码
对于两个secnarios
清空已完成周期
{
"expand": "schema,names",
"startAt": 0,
"maxResults": 50,
"total": 37875,
"issues": [
{
"id": "1190",
"key": "GDS-81",
"fields": {
"issuetype": {
"id": "2170",
"name": "Service Request with Approvals",
"subtask": false
},
"customfield_29805": {
"id": "26",
"name": "Issue - First Response",
"completedCycles": []
}
}
}
]
}
非空完成周期
{
"expand": "schema,names",
"startAt": 0,
"maxResults": 50,
"total": 37875,
"issues": [
{
"id": "1190",
"key": "GDS-81",
"fields": {
"issuetype": {
"id": "2170",
"name": "Service Request with Approvals",
"subtask": false
},
"customfield_29805": {
"id": "26",
"name": "Issue - First Response",
"completedCycles": [{"name":"abc"},{"name": "xyz"}]
}
}
}
]
}
尝试在该语句的末尾添加 getAsJsonObject()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.