Java - 如何解析空的嵌套 Json 数组

Java - How to Parse empty Nested Json Array

我正在尝试使用 (GSON) 库将 JSON 以下数据解析为 Java 中的字符串,我能够解析所有 JSON 字段数据,但 JSON数组。我想检查它是否是 null/empty 然后在 String 变量中存储空值,如果不是则存储原始值。

输入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

尝试在该语句的末尾添加 getAsJsonObject()。

  1. 您不需要显式地将 JsonElement 转换为 JsonObject 而是使用 getAsJsonArray ,一旦您获得数组,就可以遍历它的所有元素。

  2. 您还需要在检查其大小之前处理 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));
});

这是我得到的输出:

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

请在此处查看我的完整工作代码complete code

对于两个 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"}]
                }
            }
        }
    ]
 }