Example usage for com.google.gson JsonArray get

List of usage examples for com.google.gson JsonArray get

Introduction

In this page you can find the example usage for com.google.gson JsonArray get.

Prototype

public JsonElement get(int i) 

Source Link

Document

Returns the ith element of the array.

Usage

From source file:com.gullakh.gullakhandroidapp.GoogleCardsMediaActivity.java

License:Apache License

public JSONObject parse(String jsonLine) {
    String result = null;/*from w ww. j a v  a 2  s  .c om*/
    JsonElement jelement = new JsonParser().parse(jsonLine);
    JsonObject jobject = jelement.getAsJsonObject();
    JSONObject jsonObject2 = new JSONObject();
    JsonArray jarray = jobject.getAsJsonArray("result");

    Log.d(" jobject is2", String.valueOf(jarray));

    for (int i = 0; i < jarray.size(); i++) {

        jobject = jarray.get(i).getAsJsonObject();
        Log.d(" jobject is", String.valueOf(jobject));
        result = jobject.get("bankid").toString();
        Log.d(" result is", result);

        for (int i2 = 0; i2 < high_cibil.size(); i2++) {
            if (high_cibil.get(i2).equals(result)) {

                jobject.remove(String.valueOf(jobject));
            } else {
                try {
                    jsonObject2.put("result", jobject);
                } catch (JSONException e) {
                    Log.d("exception is", String.valueOf(e));
                    e.printStackTrace();
                }
            }

        }

    }

    return jsonObject2;

}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

@Override
public void update(Observable observable, Object obj) {
    try {/*from ww  w.ja va  2  s. c o  m*/
        JsonElement element = parser.parse((String) obj);
        if (element == null) {
            return;
        }

        if (element.isJsonObject()) {
            handleSingleJsonMessage((JsonObject) element);
        } else if (element.isJsonArray()) {
            JsonArray array = (JsonArray) element;
            for (int i = 0; i < array.size(); i++) {
                JsonElement e = array.get(i);
                if (e.isJsonObject()) {
                    handleSingleJsonMessage((JsonObject) e);
                }
            }
        }
    } catch (JsonSyntaxException e) {
        /*
         * There is no error handling necessary in this case. If somebody sends us invalid JSON,
         * we just ignore the packet and go ahead.
         */
        LOGGER.log(Level.SEVERE, "Can't parse JSON!", e);
    }
}

From source file:com.health.smart.ws.MobileAPI.java

@POST
@Path("/savemeasurement")
public Response saveMeasurement(String payload) {
    try {//from www  .j  a v a 2 s  .  c  o m
        JsonObject request = GsonHelper.fromString(payload);
        String token = request.getAsJsonPrimitive("token").getAsString();
        JsonArray mess = request.getAsJsonArray("measurements");
        int pid = sysEjb.validateToken(token);
        List<DayMeasurement> measurements = new ArrayList<>();
        for (int i = 0; i < mess.size(); i++) {
            DayMeasurement m = GsonHelper.fromJsonElement(mess.get(i), DayMeasurement.class);
            m.setPatientId(pid);
            measurements.add(m);
        }
        mongoEjb.saveDayMeasurement(measurements);
        JsonObject jObject = new JsonObject();
        jObject.addProperty("result", "success");
        return Response.status(200).entity(jObject.toString()).build();
    } catch (Exception e) {
        e.printStackTrace();
        return Response.status(500).entity(e.getMessage()).build();
    }
}

From source file:com.hkm.disqus.api.gson.ApplicationsUsageDeserializer.java

License:Apache License

@Override
public Usage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    // JSON element should be an array
    if (json.isJsonArray()) {
        // Create usage
        Usage usage = new Usage();

        // Iterate the array
        for (JsonElement element : json.getAsJsonArray()) {
            // Each element should be an array containing a date and int
            if (element.isJsonArray() && element.getAsJsonArray().size() == 2) {
                JsonArray jsonArray = element.getAsJsonArray();
                Date date = context.deserialize(jsonArray.get(0), Date.class);
                int count = jsonArray.get(1).getAsInt();
                usage.put(date, count);//from   w ww  . j a  v  a 2  s. c o  m
            }
        }
        return usage;
    }
    return null;
}

From source file:com.hp.mercury.ci.jenkins.plugins.oo.core.OOAccessibilityLayer.java

License:Open Source License

public String formatResult(String executionResult) {

    boolean runIsCanceled = false;
    JsonArray array = (JsonArray) (parser.parse(executionResult));
    JsonObject object;/*from  w  w  w .j a v  a2 s.  com*/
    StringBuilder resultBuilder = new StringBuilder("");

    for (int i = 0; i < array.size(); i++) {

        object = (JsonObject) array.get(i);
        JsonObject stepInfo = (JsonObject) object.get("stepInfo");
        Date date = new Date();
        try {
            long epoch = Long.parseLong(stepInfo.get("endTime").toString());
            date = new Date(epoch);
        } catch (NumberFormatException e) {
            runIsCanceled = true;
        }

        DateFormat formatter = new SimpleDateFormat("hh:mm:ss");

        String color = "blue";

        if ((stepInfo.get("transitionMessage").toString().toLowerCase().contains("failure"))
                || (stepInfo.get("responseType").toString().toLowerCase().contains(("error")))
                || (stepInfo.get("stepName").toString().toLowerCase().contains(("failure"))) || runIsCanceled) {
            color = "red";
        }

        resultBuilder.append("<font color=\"" + color + "\"><b>" + formatter.format(date) + "</b>")
                .append("\n");
        resultBuilder.append("\t<b>stepPrimaryResult</b> : ").append(object.get("stepPrimaryResult").toString())
                .append("\n");

        resultBuilder.append("\t<b>status</b> : ").append(object.get("status").toString().replace("\"", ""))
                .append("\n");
        resultBuilder.append("\t<b>rawResult</b> : ").append(object.get("rawResult").toString()).append("\n");
        resultBuilder.append("\t<b>stepResult</b> : ").append(object.get("stepResult").toString()).append("\n");
        resultBuilder.append("\t<b>stepInputs</b> : ").append(object.get("stepInputs").toString()).append("\n");

        resultBuilder.append("\t<b>stepName</b> : ")
                .append(stepInfo.get("stepName").toString().replace("\"", "")).append("\n");
        resultBuilder.append("\t<b>flowName</b> : ")
                .append(stepInfo.get("flowName").toString().replace("\"", "")).append("\n");
        resultBuilder.append("\t<b>type</b> : ").append(stepInfo.get("type").toString().replace("\"", ""))
                .append("\n");
        resultBuilder.append("\t<b>transitionMessage</b> : ")
                .append(stepInfo.get("transitionMessage").toString().replace("\"", "")).append("\n");
        resultBuilder.append("\t<b>responseType</b> : ")
                .append(stepInfo.get("responseType").toString().replace("\"", "")).append("</font>\n");
    }

    return resultBuilder.toString();

}

From source file:com.ibm.csync.internals.response.CSValueDeserializer.java

License:Open Source License

private CSKey jsonArraytoCSKey(JsonArray array) {
    CSKey key = CSKey.root;/*from   w ww. j a  v a 2 s .  c om*/
    for (int i = 0; i < array.size(); i++) {
        key = key.child(array.get(i).getAsString());
    }
    return key;
}

From source file:com.ibm.devops.dra.AbstractDevOpsAction.java

License:Open Source License

/**
 * Get a list of toolchains using given token and organization name.
 * @param token/*  www  . j a  v a2  s . c  o m*/
 * @param orgName
 * @return
 */
public static ListBoxModel getToolchainList(String token, String orgName, String environment,
        Boolean debug_mode) {

    LOGGER.setLevel(Level.INFO);

    if (debug_mode) {
        LOGGER.info("#######################");
        LOGGER.info("TOKEN:" + token);
        LOGGER.info("ORG:" + orgName);
        LOGGER.info("ENVIRONMENT:" + environment);
    }

    String orgId = getOrgId(token, orgName, environment, debug_mode);
    ListBoxModel emptybox = new ListBoxModel();
    emptybox.add("", "empty");

    if (orgId == null) {
        return emptybox;
    }

    CloseableHttpClient httpClient = HttpClients.createDefault();
    String toolchains_url = chooseToolchainsUrl(environment);
    if (debug_mode) {
        LOGGER.info("GET TOOLCHAIN LIST URL:" + toolchains_url + orgId);
    }

    HttpGet httpGet = new HttpGet(toolchains_url + orgId);

    httpGet = addProxyInformation(httpGet);

    httpGet.setHeader("Authorization", token);
    CloseableHttpResponse response = null;

    try {
        response = httpClient.execute(httpGet);
        String resStr = EntityUtils.toString(response.getEntity());

        if (debug_mode) {
            LOGGER.info("RESPONSE FROM TOOLCHAINS API:" + response.getStatusLine().toString());
        }
        if (response.getStatusLine().toString().contains("200")) {
            // get 200 response
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(resStr);
            JsonObject obj = element.getAsJsonObject();
            JsonArray items = obj.getAsJsonArray("items");
            ListBoxModel toolchainList = new ListBoxModel();

            for (int i = 0; i < items.size(); i++) {
                JsonObject toolchainObj = items.get(i).getAsJsonObject();
                String toolchainName = String.valueOf(toolchainObj.get("name")).replaceAll("\"", "");
                String toolchainID = String.valueOf(toolchainObj.get("toolchain_guid")).replaceAll("\"", "");
                toolchainList.add(toolchainName, toolchainID);
            }
            if (debug_mode) {
                LOGGER.info("TOOLCHAIN LIST:" + toolchainList);
                LOGGER.info("#######################");
            }
            if (toolchainList.isEmpty()) {
                if (debug_mode) {
                    LOGGER.info("RETURNED NO TOOLCHAINS.");
                }
                return emptybox;
            }
            return toolchainList;
        } else {
            LOGGER.info(
                    "RETURNED STATUS CODE OTHER THAN 200. RESPONSE: " + response.getStatusLine().toString());
            return emptybox;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return emptybox;
}

From source file:com.ibm.devops.dra.AbstractDevOpsAction.java

License:Open Source License

public static String getOrgId(String token, String orgName, String environment, Boolean debug_mode) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String organizations_url = chooseOrganizationsUrl(environment);
    if (debug_mode) {
        LOGGER.info("GET ORG_GUID URL:" + organizations_url + orgName);
    }//from  w  w  w.j  av  a 2  s .c  o  m

    try {
        HttpGet httpGet = new HttpGet(
                organizations_url + URLEncoder.encode(orgName, "UTF-8").replaceAll("\\+", "%20"));

        httpGet = addProxyInformation(httpGet);

        httpGet.setHeader("Authorization", token);
        CloseableHttpResponse response = null;

        response = httpClient.execute(httpGet);
        String resStr = EntityUtils.toString(response.getEntity());

        if (debug_mode) {
            LOGGER.info("RESPONSE FROM ORGANIZATIONS API:" + response.getStatusLine().toString());
        }
        if (response.getStatusLine().toString().contains("200")) {
            // get 200 response
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(resStr);
            JsonObject obj = element.getAsJsonObject();
            JsonArray resources = obj.getAsJsonArray("resources");

            if (resources.size() > 0) {
                JsonObject resource = resources.get(0).getAsJsonObject();
                JsonObject metadata = resource.getAsJsonObject("metadata");
                if (debug_mode) {
                    LOGGER.info("ORG_ID:" + String.valueOf(metadata.get("guid")).replaceAll("\"", ""));
                }
                return String.valueOf(metadata.get("guid")).replaceAll("\"", "");
            } else {
                if (debug_mode) {
                    LOGGER.info("RETURNED NO ORGANIZATIONS.");
                }
                return null;
            }

        } else {
            if (debug_mode) {
                LOGGER.info("RETURNED STATUS CODE OTHER THAN 200. RESPONSE: "
                        + response.getStatusLine().toString());
            }
            return null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.ibm.devops.dra.AbstractDevOpsAction.java

License:Open Source License

public static String getSpaceId(String token, String spaceName, String environment, Boolean debug_mode) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String spaces_url = chooseSpacesUrl(environment);
    if (debug_mode) {
        LOGGER.info("GET SPACE_GUID URL:" + spaces_url + spaceName);
    }/*from  w ww .j a  v  a 2 s  .  c  o m*/

    try {
        HttpGet httpGet = new HttpGet(
                spaces_url + URLEncoder.encode(spaceName, "UTF-8").replaceAll("\\+", "%20"));

        httpGet = addProxyInformation(httpGet);

        httpGet.setHeader("Authorization", token);
        CloseableHttpResponse response = null;

        response = httpClient.execute(httpGet);
        String resStr = EntityUtils.toString(response.getEntity());

        if (debug_mode) {
            LOGGER.info("RESPONSE FROM SPACES API:" + response.getStatusLine().toString());
        }
        if (response.getStatusLine().toString().contains("200")) {
            // get 200 response
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(resStr);
            JsonObject obj = element.getAsJsonObject();
            JsonArray resources = obj.getAsJsonArray("resources");

            if (resources.size() > 0) {
                JsonObject resource = resources.get(0).getAsJsonObject();
                JsonObject metadata = resource.getAsJsonObject("metadata");
                if (debug_mode) {
                    LOGGER.info("SPACE_ID:" + String.valueOf(metadata.get("guid")).replaceAll("\"", ""));
                }
                return String.valueOf(metadata.get("guid")).replaceAll("\"", "");
            } else {
                if (debug_mode) {
                    LOGGER.info("RETURNED NO SPACES.");
                }
                return null;
            }

        } else {
            if (debug_mode) {
                LOGGER.info("RETURNED STATUS CODE OTHER THAN 200. RESPONSE: "
                        + response.getStatusLine().toString());
            }
            return null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.ibm.devops.dra.AbstractDevOpsAction.java

License:Open Source License

public static String getAppId(String token, String appName, String orgName, String spaceName,
        String environment, Boolean debug_mode) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    String apps_url = chooseAppsUrl(environment);
    if (debug_mode) {
        LOGGER.info("GET APPS_GUID URL:" + apps_url + appName + ORG + orgName + SPACE + spaceName);
    }/*from  w  w w .ja  v  a 2  s. c o m*/

    try {
        HttpGet httpGet = new HttpGet(apps_url + URLEncoder.encode(appName, "UTF-8").replaceAll("\\+", "%20")
                + ORG + URLEncoder.encode(orgName, "UTF-8").replaceAll("\\+", "%20") + SPACE
                + URLEncoder.encode(spaceName, "UTF-8").replaceAll("\\+", "%20"));

        httpGet = addProxyInformation(httpGet);

        httpGet.setHeader("Authorization", token);
        CloseableHttpResponse response = null;

        response = httpClient.execute(httpGet);
        String resStr = EntityUtils.toString(response.getEntity());

        if (debug_mode) {
            LOGGER.info("RESPONSE FROM APPS API:" + response.getStatusLine().toString());
        }
        if (response.getStatusLine().toString().contains("200")) {
            // get 200 response
            JsonParser parser = new JsonParser();
            JsonElement element = parser.parse(resStr);
            JsonObject obj = element.getAsJsonObject();
            JsonArray resources = obj.getAsJsonArray("resources");

            if (resources.size() > 0) {
                JsonObject resource = resources.get(0).getAsJsonObject();
                JsonObject metadata = resource.getAsJsonObject("metadata");
                if (debug_mode) {
                    LOGGER.info("APP_ID:" + String.valueOf(metadata.get("guid")).replaceAll("\"", ""));
                }
                return String.valueOf(metadata.get("guid")).replaceAll("\"", "");
            } else {
                if (debug_mode) {
                    LOGGER.info("RETURNED NO APPS.");
                }
                return null;
            }

        } else {
            if (debug_mode) {
                LOGGER.info("RETURNED STATUS CODE OTHER THAN 200. RESPONSE: "
                        + response.getStatusLine().toString());
            }
            return null;
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}