简体   繁体   中英

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

Please help I am trying to fetch all tests associated with Execution in JIRA XRAY

I am getting java.lang.IllegalStateException: Not a JSON Object error at step mentioned in last below(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();

Note: inputLine prints as [{"id":100806,"status":"TODO","key":"ST_MABC-1234","rank":1}]

Actually, the response is an array of JSON objects. Therefore you cannot parse it as JSON object. You'll need to use the JSONArray class instead. Example:

    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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM