0

请帮助我正在尝试获取与 JIRA XRAY 中的执行相关的所有测试

我在下面最后提到的步骤中收到 java.lang.IllegalStateException: Not a JSON Object 错误(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}]

4

1 回答 1

1

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

    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"));
于 2022-02-23T16:54:14.483 回答