Example usage for org.json JSONObject get

List of usage examples for org.json JSONObject get

Introduction

In this page you can find the example usage for org.json JSONObject get.

Prototype

public Object get(String key) throws JSONException 

Source Link

Document

Get the value object associated with a key.

Usage

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for listForks method with optional parameters.
 *//*from  w ww .  ja va  2s.c o m*/
@Test(priority = 1, dependsOnMethods = {
        "testListForksWithMandatoryParameters" }, description = "github {listForks} integration test with optional parameters.")
public void testListForksWithOptionalParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:listForks");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/forks?sort=newest";
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listForks_optional.json");
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbResponseJsonArray = new JSONArray(esbRestResponse.getBody().get("output").toString());
    JSONArray apiResponseJsonArray = new JSONArray(apiRestResponse.getBody().get("output").toString());
    Assert.assertEquals(esbResponseJsonArray.length(), apiResponseJsonArray.length());
    if (esbResponseJsonArray.length() > 0 && apiResponseJsonArray.length() > 0) {
        JSONObject esbFirstElement = esbResponseJsonArray.getJSONObject(0);
        JSONObject apiFirstElement = apiResponseJsonArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("id"), apiFirstElement.get("id"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for listCollaborators method with mandatory parameters.
 *//*from  w w  w. j  a v  a 2  s  . c  o m*/
@Test(priority = 1, dependsOnMethods = {
        "testListForksIssuesNegativeCase" }, description = "github {listCollaborators} integration test with mandatory parameters.")
public void testListCollaboratorsWithMandatoryParameters()
        throws IOException, JSONException, InterruptedException {
    esbRequestHeadersMap.put("Action", "urn:listCollaborators");
    Thread.sleep(timeOut);
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/collaborators";
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_listCollaborators_mandatory.json");
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbResponseJsonArray = new JSONArray(esbRestResponse.getBody().get("output").toString());
    JSONArray apiResponseJsonArray = new JSONArray(apiRestResponse.getBody().get("output").toString());
    Assert.assertEquals(esbResponseJsonArray.length(), apiResponseJsonArray.length());
    if (esbResponseJsonArray.length() > 0 && apiResponseJsonArray.length() > 0) {
        JSONObject esbFirstElement = esbResponseJsonArray.getJSONObject(0);
        JSONObject apiFirstElement = apiResponseJsonArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("id"), apiFirstElement.get("id"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for getNotifications method with mandatory parameters.
 *//*from  www. j  a  v a 2  s  .  c  o  m*/
@Test(priority = 1, dependsOnMethods = {
        "testListCollaboratorsWithNegativeCase" }, description = "github {getNotifications} integration test with mandatory parameters.")
public void testGetNotificationsWithMandatoryParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:getNotifications");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_getNotifications_mandatory.json");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/notifications";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbResponseJsonArray = new JSONArray(esbRestResponse.getBody().get("output").toString());
    JSONArray apiResponseJsonArray = new JSONArray(apiRestResponse.getBody().get("output").toString());
    Assert.assertEquals(esbResponseJsonArray.length(), apiResponseJsonArray.length());
    if (esbResponseJsonArray.length() > 0 && apiResponseJsonArray.length() > 0) {
        JSONObject esbFirstElement = esbResponseJsonArray.getJSONObject(0);
        JSONObject apiFirstElement = apiResponseJsonArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("id"), apiFirstElement.get("id"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for getNotifications method with optional parameters.
 *///from   w  ww .  j a v a 2 s  .co  m
@Test(priority = 1, dependsOnMethods = {
        "testGetNotificationsWithMandatoryParameters" }, description = "github {getNotifications} integration test with optional parameters.")
public void testGetNotificationsWithOptionalParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:getNotifications");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_getNotifications_optional.json");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/notifications?all=true";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbResponseJsonArray = new JSONArray(esbRestResponse.getBody().get("output").toString());
    JSONArray apiResponseJsonArray = new JSONArray(apiRestResponse.getBody().get("output").toString());
    Assert.assertEquals(esbResponseJsonArray.length(), apiResponseJsonArray.length());
    if (esbResponseJsonArray.length() > 0 && apiResponseJsonArray.length() > 0) {
        JSONObject esbFirstElement = esbResponseJsonArray.getJSONObject(0);
        JSONObject apiFirstElement = apiResponseJsonArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("id"), apiFirstElement.get("id"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for getRepositoryNotifications method with mandatory parameters.
 *///from w ww  .j  av  a 2s . co  m
@Test(priority = 1, dependsOnMethods = {
        "testGetNotificationsWithNegativeCase" }, description = "github {getRepositoryNotifications} integration test with mandatory parameters.")
public void testGetRepositoryNotificationsWithMandatoryParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:getRepositoryNotifications");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_getRepositoryNotifications_mandatory.json");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/notifications";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbResponseJsonArray = new JSONArray(esbRestResponse.getBody().get("output").toString());
    JSONArray apiResponseJsonArray = new JSONArray(apiRestResponse.getBody().get("output").toString());
    Assert.assertEquals(esbResponseJsonArray.length(), apiResponseJsonArray.length());
    if (esbResponseJsonArray.length() > 0 && apiResponseJsonArray.length() > 0) {
        JSONObject esbFirstElement = esbResponseJsonArray.getJSONObject(0);
        JSONObject apiFirstElement = apiResponseJsonArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("id"), apiFirstElement.get("id"));
    }
}

From source file:org.wso2.carbon.connector.integration.test.github.GithubConnectorIntegrationTest.java

/**
 * Positive test case for getRepositoryNotifications method with optional parameters.
 *//*www.j a v  a 2 s . c o m*/
@Test(priority = 1, dependsOnMethods = {
        "testGetRepositoryNotificationsWithMandatoryParameters" }, description = "github {getRepositoryNotifications} integration test with optional parameters.")
public void testGetRepositoryNotificationsWithOptionalParameters() throws IOException, JSONException {
    esbRequestHeadersMap.put("Action", "urn:getRepositoryNotifications");
    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_getRepositoryNotifications_optional.json");
    String apiEndPoint = connectorProperties.getProperty("githubApiUrl") + "/repos/"
            + connectorProperties.getProperty("owner") + "/" + connectorProperties.getProperty("repo")
            + "/notifications?all=true";
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);
    JSONArray esbResponseJsonArray = new JSONArray(esbRestResponse.getBody().get("output").toString());
    JSONArray apiResponseJsonArray = new JSONArray(apiRestResponse.getBody().get("output").toString());
    Assert.assertEquals(esbResponseJsonArray.length(), apiResponseJsonArray.length());
    if (esbResponseJsonArray.length() > 0 && apiResponseJsonArray.length() > 0) {
        JSONObject esbFirstElement = esbResponseJsonArray.getJSONObject(0);
        JSONObject apiFirstElement = apiResponseJsonArray.getJSONObject(0);
        Assert.assertEquals(esbFirstElement.get("id"), apiFirstElement.get("id"));
    }
}

From source file:io.swagger.client.ApiClient.java

/**
 * Deserialize response body to Java object, according to the return type and
 * the Content-Type response header./*from w ww.  j a v  a 2s .  co  m*/
 *
 * @param <T> Type
 * @param response HTTP response
 * @param returnType The type of the Java object
 * @return The deserialized Java object
 * @throws ApiException If fail to deserialize response body, i.e. cannot read response body
 *   or the Content-Type of the response is not supported.
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(Response response, Type returnType) throws ApiException, IOException {
    if (response == null || returnType == null) {
        return null;
    }

    if ("byte[]".equals(returnType.toString())) {
        // Handle binary response (byte array).
        try {
            return (T) response.body().bytes();
        } catch (IOException e) {
            throw new ApiException(e);
        }
    } else if (returnType.equals(File.class)) {
        // Handle file downloading.
        return (T) downloadFileFromResponse(response);
    }

    String respBody;
    try {
        if (response.body() != null)
            respBody = response.body().string();
        else
            respBody = null;
    } catch (IOException e) {
        throw new ApiException(e);
    }

    if (respBody == null || "".equals(respBody)) {
        return null;
    }

    String contentType = response.headers().get("Content-Type");
    if (contentType == null) {
        // ensuring a default content type
        contentType = "application/json";
    } else if (contentType.equals("application/xml")) {
        if (XML.toJSONObject(respBody).has("TaskList")) {
            JSONObject rootElement = (JSONObject) XML.toJSONObject(respBody).get("TaskList");
            return this.json.deserialize(
                    ((JSONArray) ((JSONObject) rootElement.get("Tasks")).get("Tasks")).toString(), returnType);
        }
    }
    if (isJsonMime(contentType)) {
        if (new JSONObject(respBody).has("Tasks")) {
            return json.deserialize(new JSONObject(respBody).get("Tasks").toString(), returnType);
        } else {
            return json.deserialize(respBody, returnType);
        }
    } else if (returnType.equals(String.class)) {
        // Expecting string, return the raw response body.
        return (T) respBody;
    } else {
        throw new ApiException("Content type \"" + contentType + "\" is not supported for type: " + returnType,
                response.code(), response.headers().toMultimap(), respBody);
    }
}

From source file:com.soomla.store.domain.data.StaticPriceModel.java

/**
 * docs in {@link AbstractPriceModel#toJSONObject()}
 *//*from w  w w.ja v  a 2  s. c o m*/
@Override
public JSONObject toJSONObject() throws JSONException {
    JSONObject parentJsonObject = super.toJSONObject();
    JSONObject jsonObject = new JSONObject();

    Iterator<?> keys = parentJsonObject.keys();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        jsonObject.put(key, parentJsonObject.get(key));
    }

    JSONObject currencyValues = new JSONObject();
    for (String key : mCurrencyValue.keySet()) {
        currencyValues.put(key, mCurrencyValue.get(key));
    }
    jsonObject.put(JSONConsts.GOOD_PRICE_MODEL_VALUES, currencyValues);

    return jsonObject;
}

From source file:com.heliosapm.opentsdb.client.opentsdb.OpenTsdbPutResponseHandler.java

/**
 * Handles reported errors in the metrics post response
 * @param arr The JSON array of errors//from   w  w w  .  ja va 2s.c  om
 * @param log The logger to log the errors with
 */
protected static void processErrors(final JSONArray arr, final Logger log) {
    if (arr == null || arr.length() < 1)
        return;
    try {
        final int sz = arr.length();
        for (int i = 0; i < sz; i++) {
            JSONObject dp = arr.getJSONObject(i).getJSONObject("datapoint");
            log.error("BAD METRIC: metric:{}, tags:{}", dp.get("metric"), dp.get("tags"));
            // TODO: build a metric name that can be used to filter submissions 
            // so these don't get sent again.
        }
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
}

From source file:com.nosoop.json.VDF.java

/**
 * Recursively searches for JSONObjects, checking if they should be
 * formatted as arrays, then converted.// ww  w. j  a  va 2 s .  c  o  m
 *
 * @param object An input JSONObject converted from VDF.
 * @return JSONObject containing the input JSONObject with objects changed
 * to arrays where applicable.
 * @throws JSONException
 */
private static JSONObject convertVDFArrays(JSONObject object) throws JSONException {
    JSONObject resp = new JSONObject();

    if (!object.keys().hasNext()) {
        return resp;
    }

    for (@SuppressWarnings("unchecked")
    Iterator<String> iter = object.keys(); iter.hasNext();) {
        String name = iter.next();
        JSONObject thing = object.optJSONObject(name);

        if (thing != null) {
            // Note:  Empty JSONObjects are also treated as arrays.
            if (containsVDFArray(thing)) {
                @SuppressWarnings("unchecked")
                Iterator<String> iter2 = thing.keys();
                List<String> sortingKeys = new ArrayList<String>();
                while (iter2.hasNext())
                    sortingKeys.add(iter2.next());
                Collections.sort(sortingKeys, new Comparator<String>() {
                    // Integers-as-strings comparator.
                    @Override
                    public int compare(String t, String t1) {
                        int i = Integer.parseInt(t), i1 = Integer.parseInt(t1);
                        return i - i1;
                    }
                });

                JSONArray sortedKeys = new JSONArray(sortingKeys);

                if (sortedKeys.length() > 0) {
                    JSONArray sortedObjects = thing.toJSONArray(sortedKeys);

                    for (int i = 0; i < sortedObjects.length(); i++) {
                        JSONObject arrayObject = sortedObjects.getJSONObject(i);

                        /**
                         * See if any values are also JSONObjects that
                         * should be arrays.
                         */
                        sortedObjects.put(i, convertVDFArrays(arrayObject));
                    }

                    /**
                     * If this JSONObject represents a non-empty array in
                     * VDF format, convert it to a JSONArray.
                     */
                    resp.put(name, sortedObjects);
                } else {
                    /**
                     * If this JSONObject represents an empty array, give it
                     * an empty JSONArray.
                     */
                    resp.put(name, new JSONArray());
                }
            } else {
                /**
                 * If this JSONObject is not a VDF array, see if its values
                 * are before adding.
                 */
                resp.put(name, convertVDFArrays(thing));
            }
        } else {
            /**
             * It's a plain data value. Add it in.
             */
            resp.put(name, object.get(name));
        }
    }

    /**
     * Return the converted JSONObject.
     */
    return resp;
}