Example usage for org.json JSONArray getJSONObject

List of usage examples for org.json JSONArray getJSONObject

Introduction

In this page you can find the example usage for org.json JSONArray getJSONObject.

Prototype

public JSONObject getJSONObject(int index) throws JSONException 

Source Link

Document

Get the JSONObject associated with an index.

Usage

From source file:produvia.com.scanner.DevicesActivity.java

private void updateServiceDeviceDatabase(JSONObject data) {
    try {//from   w  ww . j  ava  2 s .  c om
        //first add the services to the devices:
        JSONArray services = data.getJSONArray("services");
        for (int i = 0; i < services.length(); i++) {
            JSONObject service = services.getJSONObject(i);
            String device_id = service.getString("device_id");
            JSONObject device = data.getJSONObject("devices_info").getJSONObject(device_id);
            if (!device.has("services"))
                device.put("services", new JSONObject());
            device.getJSONObject("services").put(service.getString("id"), service);
        }

        JSONObject devices = data.getJSONObject("devices_info");
        //loop over the devices and merge them into the device display:
        for (Iterator<String> iter = devices.keys(); iter.hasNext();) {
            String device_id = iter.next();
            JSONObject device = devices.getJSONObject(device_id);
            //if a device card is already present - just merge the data:
            boolean found = false;
            int network_card_idx = -1;
            for (int i = 0; i < mDevices.size(); i++) {
                CustomListItem cli = mDevices.get(i);
                if (cli instanceof DeviceCard && ((DeviceCard) cli).getId().equals(device_id)) {
                    ((DeviceCard) cli).updateInfo(device);
                    found = true;
                    break;
                } else if (cli.getDescription().equals(device.getString("network_id"))) {
                    network_card_idx = i;
                }
            }

            if (!found) {
                if (network_card_idx < 0) {
                    JSONObject network = data.getJSONObject("networks_info")
                            .getJSONObject(device.getString("network_id"));
                    String name = "";
                    if (network.has("name") && network.getString("name") != null)
                        name = network.getString("name");
                    network_card_idx = addNetworkCard(name, device.getString("network_id"),
                            network.getBoolean("user_inside_network"));
                }
                network_card_idx += 1;
                //find the correct index for the card sorted by last seen:
                for (; network_card_idx < mDevices.size(); network_card_idx++) {
                    CustomListItem cli = mDevices.get(network_card_idx);
                    if (!(cli instanceof DeviceCard))
                        break;
                    if (((DeviceCard) cli).getLastSeen()
                            .compareTo(DeviceCard.getLastSeenFromString(device.getString("last_seen"))) < 0)
                        break;
                }

                DeviceCard dc = new DeviceCard(device);
                mDevices.add(network_card_idx, dc);
            }
        }
        notifyDataSetChanged();

    } catch (JSONException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

From source file:com.vk.sdkweb.api.model.VKList.java

/**
 * Fills list according with data in {@code from}.
 * @param from an array of items in the list. You can use null.
 * @param creator interface implementation to parse objects.
 *//*from   w w  w .j  a  va2  s  . com*/
public void fill(JSONArray from, Parser<? extends T> creator) {
    if (from != null) {
        for (int i = 0; i < from.length(); i++) {
            try {
                T object = creator.parseObject(from.getJSONObject(i));
                if (object != null) {
                    items.add(object);
                }
            } catch (Exception e) {
                if (VKSdk.DEBUG)
                    e.printStackTrace();
            }
        }
    }
}

From source file:org.wso2.carbon.connector.integration.test.quickbooks.QuickbooksConnectorIntegrationTest.java

/**
 * Negative test case for createAccount.
 *///from   ww w.j  ava  2  s. c o  m
@Test(priority = 1, description = "quickbooks {createAccount} integration test with negative case.")
public void testCreateAccountWithNegativeCase() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:createAccount");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_createAccount_negative.json");

    JSONArray esbResponseArray = esbRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/v3/company/" + companyId + "/account/";
    String OAuthHeader = getOAuthHeader("POST", apiEndPoint);
    apiRequestHeadersMap.put("Authorization", OAuthHeader);
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "POST", apiRequestHeadersMap,
            "api_createAccount_negative.json");

    JSONArray apiResponseArray = apiRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    Assert.assertEquals(esbRestResponse.getHttpStatusCode(), apiRestResponse.getHttpStatusCode());
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("Message"),
            esbResponseArray.getJSONObject(0).getString("Message"));
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("code"),
            esbResponseArray.getJSONObject(0).getString("code"));
}

From source file:org.wso2.carbon.connector.integration.test.quickbooks.QuickbooksConnectorIntegrationTest.java

/**
 * Negative test case for createCustomer.
 *///  w  ww.j  ava  2  s .c  o m
@Test(priority = 1, description = "quickbooks {createCustomer} integration test with negative case.")
public void testCreateCustomerWithNegativeCase() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:createCustomer");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_createCustomer_negative.json");

    JSONArray esbResponseArray = esbRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/v3/company/" + companyId + "/customer/";
    String OAuthHeader = getOAuthHeader("POST", apiEndPoint);
    apiRequestHeadersMap.put("Authorization", OAuthHeader);
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "POST", apiRequestHeadersMap,
            "api_createCustomer_negative.json");

    JSONArray apiResponseArray = apiRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    Assert.assertEquals(esbRestResponse.getHttpStatusCode(), apiRestResponse.getHttpStatusCode());
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("Message"),
            esbResponseArray.getJSONObject(0).getString("Message"));
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("code"),
            esbResponseArray.getJSONObject(0).getString("code"));
}

From source file:org.wso2.carbon.connector.integration.test.quickbooks.QuickbooksConnectorIntegrationTest.java

/**
 * Negative test case for createVendor./*from  w  w w . ja  va 2  s .c  o m*/
 */
@Test(priority = 1, description = "quickbooks {createVendor} integration test with negative case.")
public void testCreateVendorWithNegativeCase() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:createVendor");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_createVendor_negative.json");

    JSONArray esbResponseArray = esbRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/v3/company/" + companyId + "/vendor/";
    String OAuthHeader = getOAuthHeader("POST", apiEndPoint);
    apiRequestHeadersMap.put("Authorization", OAuthHeader);
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "POST", apiRequestHeadersMap,
            "api_createVendor_negative.json");

    JSONArray apiResponseArray = apiRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    Assert.assertEquals(esbRestResponse.getHttpStatusCode(), apiRestResponse.getHttpStatusCode());
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("Message"),
            esbResponseArray.getJSONObject(0).getString("Message"));
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("code"),
            esbResponseArray.getJSONObject(0).getString("code"));
}

From source file:org.wso2.carbon.connector.integration.test.quickbooks.QuickbooksConnectorIntegrationTest.java

/**
 * Negative test case for createItem.//from   w  w  w .  j a  va2s .  com
 */
@Test(priority = 1, description = "quickbooks {createItem} integration test with negative case.")
public void testCreateItemWithNegativeCase() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:createItem");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_createItem_negative.json");

    JSONArray esbResponseArray = esbRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/v3/company/" + companyId + "/item/";
    String OAuthHeader = getOAuthHeader("POST", apiEndPoint);
    apiRequestHeadersMap.put("Authorization", OAuthHeader);
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "POST", apiRequestHeadersMap,
            "api_createItem_negative.json");

    JSONArray apiResponseArray = apiRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    Assert.assertEquals(esbRestResponse.getHttpStatusCode(), apiRestResponse.getHttpStatusCode());
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("Message"),
            esbResponseArray.getJSONObject(0).getString("Message"));
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("code"),
            esbResponseArray.getJSONObject(0).getString("code"));
}

From source file:org.wso2.carbon.connector.integration.test.quickbooks.QuickbooksConnectorIntegrationTest.java

/**
 * Positive test case for createPurchase method with mandatory parameters.
 *///from   www. j a  va2  s .c  om
@Test(priority = 1, dependsOnMethods = { "testCreateAccountWithMandatoryParameters",
        "testCreateAccountWithOptionalParameters" }, description = "quickbooks {createPurchase} integration test with mandatory parameters.")
public void tesCreatePurchaseWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:createPurchase");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_createPurchase_mandatory.json");

    JSONObject esbResponseObject = esbRestResponse.getBody().getJSONObject("Purchase");
    String purchaseId = esbResponseObject.getString("Id");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/v3/company/" + companyId + "/purchase/"
            + purchaseId;
    String OAuthHeader = getOAuthHeader("GET", apiEndPoint);

    apiRequestHeadersMap.put("Authorization", OAuthHeader);

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);

    JSONObject apiResponseObject = apiRestResponse.getBody().getJSONObject("Purchase");
    JSONArray apiLineArray = apiResponseObject.getJSONArray("Line");

    Assert.assertEquals(connectorProperties.getProperty("expenseAccountRef"), apiLineArray.getJSONObject(0)
            .getJSONObject("AccountBasedExpenseLineDetail").getJSONObject("AccountRef").getString("value"));
    Assert.assertEquals(connectorProperties.getProperty("bankAccoutId"),
            apiResponseObject.getJSONObject("AccountRef").getString("value"));
    Assert.assertEquals(esbResponseObject.getJSONObject("MetaData").getString("CreateTime"),
            apiResponseObject.getJSONObject("MetaData").getString("CreateTime"));

}

From source file:org.wso2.carbon.connector.integration.test.quickbooks.QuickbooksConnectorIntegrationTest.java

/**
 * Negative test case for createPurchase.
 */// w ww.j a v  a  2s .  c  o  m
@Test(priority = 1, description = "quickbooks {createPurchase} integration test with negative case.")
public void testCreatePurchaseWithNegativeCase() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:createPurchase");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_createPurchase_negative.json");

    JSONArray esbResponseArray = esbRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/v3/company/" + companyId + "/purchase/";
    String OAuthHeader = getOAuthHeader("POST", apiEndPoint);
    apiRequestHeadersMap.put("Authorization", OAuthHeader);
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "POST", apiRequestHeadersMap,
            "api_createPurchase_negative.json");

    JSONArray apiResponseArray = apiRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    Assert.assertEquals(esbRestResponse.getHttpStatusCode(), apiRestResponse.getHttpStatusCode());
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("Message"),
            esbResponseArray.getJSONObject(0).getString("Message"));
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("code"),
            esbResponseArray.getJSONObject(0).getString("code"));
}

From source file:org.wso2.carbon.connector.integration.test.quickbooks.QuickbooksConnectorIntegrationTest.java

/**
 * Positive test case for createPurchaseOrder method with mandatory parameters.
 *///from   w  w  w  .  j  ava2s.co  m
@Test(priority = 1, dependsOnMethods = { "tesCreateItemWithOptionalParameters",
        "testCreateVendorWithOptionalParameters" }, description = "quickbooks {createPurchaseOrder} integration test with mandatory parameters.")
public void tesCreatePurchaseOrderWithMandatoryParameters() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:createPurchaseOrder");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_createPurchaseOrder_mandatory.json");

    JSONObject esbResponseObject = esbRestResponse.getBody().getJSONObject("PurchaseOrder");
    String purchaseOrderId = esbResponseObject.getString("Id");
    connectorProperties.setProperty("purchaseOrderId", purchaseOrderId);

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/v3/company/" + companyId
            + "/purchaseorder/" + purchaseOrderId;
    String OAuthHeader = getOAuthHeader("GET", apiEndPoint);

    apiRequestHeadersMap.put("Authorization", OAuthHeader);

    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "GET", apiRequestHeadersMap);

    JSONObject apiResponseObject = apiRestResponse.getBody().getJSONObject("PurchaseOrder");
    JSONArray apiLineArray = apiResponseObject.getJSONArray("Line");

    Assert.assertEquals(connectorProperties.getProperty("ItemRef2"), apiLineArray.getJSONObject(0)
            .getJSONObject("ItemBasedExpenseLineDetail").getJSONObject("ItemRef").getString("value"));
    Assert.assertEquals(connectorProperties.getProperty("vendorRef"),
            apiResponseObject.getJSONObject("VendorRef").getString("value"));
    Assert.assertEquals(esbResponseObject.getJSONObject("MetaData").getString("CreateTime"),
            apiResponseObject.getJSONObject("MetaData").getString("CreateTime"));

}

From source file:org.wso2.carbon.connector.integration.test.quickbooks.QuickbooksConnectorIntegrationTest.java

/**
 * Negative test case for createPurchaseOrder.
 */// ww w. j  a  va2  s .  c  o m
@Test(priority = 1, dependsOnMethods = {
        "testCreateVendorWithOptionalParameters" }, description = "quickbooks {createPurchaseOrder} integration test with negative case.")
public void testCreatePurchaseOrderWithNegativeCase() throws IOException, JSONException {

    esbRequestHeadersMap.put("Action", "urn:createPurchaseOrder");

    RestResponse<JSONObject> esbRestResponse = sendJsonRestRequest(proxyUrl, "POST", esbRequestHeadersMap,
            "esb_createPurchaseOrder_negative.json");

    JSONArray esbResponseArray = esbRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/v3/company/" + companyId
            + "/purchaseorder/";
    String OAuthHeader = getOAuthHeader("POST", apiEndPoint);
    apiRequestHeadersMap.put("Authorization", OAuthHeader);
    RestResponse<JSONObject> apiRestResponse = sendJsonRestRequest(apiEndPoint, "POST", apiRequestHeadersMap,
            "api_createPurchaseOrder_negative.json");

    JSONArray apiResponseArray = apiRestResponse.getBody().getJSONObject("Fault").getJSONArray("Error");

    Assert.assertEquals(esbRestResponse.getHttpStatusCode(), apiRestResponse.getHttpStatusCode());
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("Message"),
            esbResponseArray.getJSONObject(0).getString("Message"));
    Assert.assertEquals(apiResponseArray.getJSONObject(0).getString("code"),
            esbResponseArray.getJSONObject(0).getString("code"));
}