JIRA Xray- 进行 Rest API 调用以获取执行详细信息时不是 JSON 对象错误

JIRA Xray- Not a JSON Object error while making Rest API call to get Execution details

请帮忙 我正在尝试在 JIRA XRAY

中获取与执行相关的所有测试

我收到 java.lang.IllegalStateException:在下面最后提到的步骤中出现 JSON 对象错误(element.getAsJsonObject();)

String urlString=baseServerURL+"/rest/raven/latest/api/testexec/"+executionCycleKey+"/test";
                System.out.println(urlString);
                HttpURLConnection con = getConnection(urlString, "GET");
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer content = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    System.out.println(inputLine.toString());
                    content.append(inputLine);
                }
                in.close();
                con.disconnect();
                Gson g = new Gson();
                JsonElement element = g.fromJson(content.toString(), JsonElement.class);
                JsonObject jsonObj = element.getAsJsonObject();

注意:inputLine 打印为 [{"id":100806,"status":"TODO","key":"ST_MABC-1234","rank":1}]

实际上,响应是 JSON 个对象的数组。因此您不能将其解析为 JSON 对象。 您需要改用 JSON 数组 class。 示例:

    String raw = "[{\"id\":100806,\"status\":\"TODO\",\"key\":\"ST_MABC-1234\",\"rank\":1} ]";
    System.out.println(raw);
    JSONArray arr = new JSONArray(raw);
    System.out.println( ((JSONObject)(arr.get(0))).get("id"));