英文:
Java - How to Parse empty Nested Json Array
问题
JsonObject object = (JsonObject) new JsonParser().parse(jsonResponse);
JsonArray issuesArray = object.getAsJsonArray("issues");
for (int i = 0; i < issuesArray.size(); i++) {
JsonObject currentissues = issuesArray.get(i).getAsJsonObject();
String Issue_Id = currentissues.get("id").getAsString();
String Issue_Key = currentissues.get("key").getAsString();
String Issue_Type = currentissues.get("fields").getAsJsonObject().get("issuetype").getAsJsonObject().get("name").getAsString();
JsonElement customfield = currentissues.get("fields").getAsJsonObject().get("customfield_29805");
JsonArray completedCyclesArray = customfield.getAsJsonObject().getAsJsonArray("completedCycles");
String Issue_FirstResponseStartTime = (completedCyclesArray.size() > 0) ? completedCyclesArray.get(0).getAsString() : "NULL";
}
请注意,我已经根据您提供的内容对代码进行了修改,以解决您遇到的问题。这段代码解析了您的JSON数据并提取了所需的字段,同时处理了您在customfield_29805
中的情况。如果completedCycles
数组不为空,则将其第一个值存储在Issue_FirstResponseStartTime
变量中;如果为空,则将其设置为"NULL"。
英文:
I am trying to Parse below JSON data to String in Java using (GSON) Library, I am able to parse all JSON fields data except one of the JSON Array. I want to check if it's null/empty then in String variable store null value, if it's not then store the original value.
Input JSON Data:
{
"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": []
}
}
}
]
}
Code that I have done so far,
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";
}
However when I execute code I get below error on line :JsonObject customfield
java.lang.ClassCastException: com.google.gson.JsonNull cannot be cast to com.google.gson.JsonObject
答案1
得分: 1
-
你不需要显式地将 JsonElement 转换为 JsonObject,而是使用 getAsJsonArray。一旦获得数组,就可以迭代其中的所有元素。
-
在检查 completedCyclesArray 的大小之前,你还需要处理 completedCyclesArray 的空值检查,否则会导致 NPE(空指针异常),我已经修复了这个问题。以下是修改后的可工作代码:
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(jsonResponse).getAsJsonArray();
for (JsonElement e : array) {
JsonObject currentissues = e.getAsJsonObject();
String Issue_Id = currentissues.get("id").getAsString().replace("\"", "");
String Issue_Key = currentissues.get("key").getAsString().replace("\"", "");
String Issue_Type = currentissues.get("fields").getAsJsonObject().get("issuetype").getAsJsonObject().get("name").getAsString();
JsonObject customfield = currentissues.get("fields").getAsJsonObject().get("customfield_29805").getAsJsonObject();
JsonArray completedCyclesArray = customfield.getAsJsonArray("completedCycles");
String Issue_FirstResponseStartTime = (completedCyclesArray != null && completedCyclesArray.size() > 0) ? completedCyclesArray.toString() : "NULL";
}
JsonObject object = new JsonParser().parse(jsonResponse).getAsJsonObject();
JsonArray issuesArray = object.getAsJsonArray("issues");
String expand = object.get("expand").getAsString();
String startAt = object.get("startAt").getAsString();
String maxResults = object.get("maxResults").getAsString();
String total = object.get("total").getAsString();
System.out.println(String.format("expand %s , startAt %s, maxResults %s, total %s", expand, startAt, maxResults, total));
issuesArray.forEach(currentissues -> {
String Issue_Id = currentissues.get("id").getAsString().replace("\"", "");
String Issue_Key = currentissues.get("key").getAsString().replace("\"", "");
String Issue_Type = currentissues.get("fields").getAsJsonObject().get("issuetype").getAsJsonObject().get("name").getAsString();
JsonObject customfield = currentissues.get("fields").getAsJsonObject().get("customfield_29805").getAsJsonObject();
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));
});
希望这些代码对你有所帮助。
英文:
-
you dont need to explicitly , cast JsonElement to JsonObject instead use getAsJsonArray , Once you get your array, you can iterate through all the elements of it.
-
You also need to handle null check for completedCyclesArray before checking its siz else it will give you the NPE , I have fixed that as well.
Please find the modified working code as belowJsonParser 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"; }
}
Please find my working solution for the updated json request(which not an array but nested json request)
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));
});
and this is the output I got :
> expand "schema,names" , startAt 0, maxResults 50, total 37875 Issue_Id
> 1190 , Issue_Key GDS-81, Issue_Type Service Request with Approvals,
> Issue_FirstResponseStartTime NULL
Please see my complete working code here complete code
for both the secnarios
Empty 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": []
}
}
}
]
}
Non Empty 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"}]
}
}
}
]
}
答案2
得分: 0
尝试在该语句的末尾添加 getAsJsonObject()。
英文:
Try adding getAsJsonObject() at the end of that statement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论