Example usage for org.json JSONArray length

List of usage examples for org.json JSONArray length

Introduction

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

Prototype

public int length() 

Source Link

Document

Get the number of elements in the JSONArray, included nulls.

Usage

From source file:org.bcsphere.bluetooth.BluetoothSam42.java

private void addCharacteristics(MutableBluetoothGattService bluetoothGattService, JSONArray jsonCharacteristics,
        CallbackContext callbackContext) {
    int characterLength = jsonCharacteristics.length();
    for (int j = 0; j < characterLength; j++) {
        String characteristicValueType = Tools.getData(jsonCharacteristics, j, Tools.CHARACTERISTIC_VALUE_TYPE);
        String characteristicValue = Tools.getData(jsonCharacteristics, j, Tools.CHARACTERISTIC_VALUE);
        String strCharacteristicUUID = Tools.getData(jsonCharacteristics, j, Tools.CHARACTERISTIC_UUID);
        String[] args = new String[] { characteristicValueType, characteristicValue, strCharacteristicUUID };
        if (!isNullOrEmpty(args, callbackContext)) {
            return;
        }/*w ww . ja va  2s. c o  m*/
        UUID characteristicUUID = UUID.fromString(strCharacteristicUUID);
        int characterProperty = Tools
                .encodeProperty(Tools.getArray(jsonCharacteristics, j, Tools.CHARACTERISTIC_PROPERTY));
        int characterPermission = Tools
                .encodePermission(Tools.getArray(jsonCharacteristics, j, Tools.CHARACTERISTIC_PERMISSION));
        MutableBluetoothGattCharacteristic bluetoothGattCharacteristic = createCharacteristic(
                characteristicUUID, characterProperty, characterPermission, characteristicValueType,
                characteristicValue);
        JSONArray jsonDescriptors = Tools.getArray(jsonCharacteristics, j, Tools.DESCRIPTORS);
        addDescriptors(bluetoothGattCharacteristic, jsonDescriptors, callbackContext);
        bluetoothGattService.addCharacteristic(bluetoothGattCharacteristic);
    }
}

From source file:org.bcsphere.bluetooth.BluetoothSam42.java

private void addDescriptors(MutableBluetoothGattCharacteristic bluetoothGattCharacteristic,
        JSONArray jsonDescriptors, CallbackContext callbackContext) {
    int descLength = jsonDescriptors.length();
    for (int k = 0; k < descLength; k++) {
        String descriptorValue = Tools.getData(jsonDescriptors, k, Tools.DESCRIPTOR_VALUE);
        String strDescriptorUUID = Tools.getData(jsonDescriptors, k, Tools.DESCRIPTOR_UUID);
        String descriptorValueType = Tools.getData(jsonDescriptors, k, Tools.DESCRIPTOR_VALUE_TYPE);
        UUID descriptorsUUID = UUID.fromString(strDescriptorUUID);
        int descriptorsPermission = Tools
                .encodePermission(Tools.getArray(jsonDescriptors, k, Tools.DESCRIPTOR_PERMISSION));
        MutableBluetoothGattDescriptor bluetoothGattDescriptor = createDescriptor(descriptorsUUID,
                descriptorsPermission, descriptorValueType, descriptorValue);
        bluetoothGattCharacteristic.addDescriptor(bluetoothGattDescriptor);
    }/*from   ww w .j a v  a2s  .  c o  m*/
}

From source file:com.namelessdev.mpdroid.cover.ItunesCover.java

@Override
public String[] getCoverUrl(final AlbumInfo albumInfo) throws Exception {
    final String response;
    final JSONObject jsonRootObject;
    final JSONArray jsonArray;
    String coverUrl;/*from w w  w  . j a v a  2s  .com*/
    JSONObject jsonObject;

    try {
        response = executeGetRequest("https://itunes.apple.com/search?term=" + albumInfo.getAlbum() + ' '
                + albumInfo.getArtist() + "&limit=5&media=music&entity=album");
        jsonRootObject = new JSONObject(response);
        jsonArray = jsonRootObject.getJSONArray("results");
        for (int i = 0; i < jsonArray.length(); i++) {
            jsonObject = jsonArray.getJSONObject(i);
            coverUrl = jsonObject.getString("artworkUrl100");
            if (coverUrl != null) {
                // Based on some tests even if the cover art size returned
                // is 100x100
                // Bigger versions also exists.
                return new String[] { coverUrl.replace("100x100", "600x600") };
            }
        }

    } catch (final Exception e) {
        if (CoverManager.DEBUG) {
            Log.e(TAG, "Failed to get cover URL from " + getName(), e);
        }
    }

    return new String[0];
}

From source file:mr.robotto.engine.loader.components.shader.MrShaderProgramLoader.java

private void loadUniforms(MrShaderProgram program) throws JSONException {
    JSONArray uniformsJsonArray = mRoot.getJSONArray("Uniforms");
    for (int i = 0; i < uniformsJsonArray.length(); i++) {
        JSONObject uniformJson = uniformsJsonArray.getJSONObject(i);
        MrUniformLoader uniformLoader = new MrUniformLoader(uniformJson);
        MrUniform uniform = uniformLoader.parse();
        program.addUniform(uniform);/*from w  w w.  j  a va2  s  .c om*/
    }
}

From source file:mr.robotto.engine.loader.components.shader.MrShaderProgramLoader.java

private void loadAttributes(MrShaderProgram program) throws JSONException {
    JSONArray attributesJsonArray = mRoot.getJSONArray("Attributes");
    for (int i = 0; i < attributesJsonArray.length(); i++) {
        JSONObject attributeJson = attributesJsonArray.getJSONObject(i);
        MrAttributeLoader attributeLoader = new MrAttributeLoader(attributeJson);
        MrAttribute attribute = attributeLoader.parse();
        program.addAttribute(attribute);
    }//from w w  w  .ja v a2  s .c  om
}

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

private void updateServiceDeviceDatabase(JSONObject data) {
    try {//from  w ww. j av a2  s.c  o  m
        //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 ww.j a va 2  s  .co m
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

/**
 * Positive test case for query method with mandatory parameters.
 *//*from ww  w  . j av  a 2 s.c  om*/
@Test(priority = 1, description = "quickbooks {query} integration test with mandatory parameters.")
public void testQueryWithMandatoryParameters() throws IOException, JSONException {

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

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

    JSONObject esbResponseObject = esbRestResponse.getBody().getJSONObject("QueryResponse");
    JSONArray esbAccountArray = esbResponseObject.getJSONArray("Account");

    String apiEndPoint = connectorProperties.getProperty("apiUrl") + "/v3/company/" + companyId
            + "/query?query=select%20*%20from%20Account%20ORDERBY%20Id%20MAXRESULTS%2010";
    String OAuthHeader = getOAuthHeader("GET", apiEndPoint);

    apiRequestHeadersMap.put("Authorization", OAuthHeader);

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

    JSONObject apiResponseObject = apiRestResponse.getBody().getJSONObject("QueryResponse");
    JSONArray apiAccountArray = apiResponseObject.getJSONArray("Account");

    Assert.assertEquals(esbAccountArray.length(), apiAccountArray.length());
    Assert.assertEquals(esbAccountArray.getJSONObject(0).getString("Name"),
            apiAccountArray.getJSONObject(0).getString("Name"));
    Assert.assertEquals(esbAccountArray.getJSONObject(0).getString("AccountType"),
            apiAccountArray.getJSONObject(0).getString("AccountType"));
    Assert.assertEquals(esbResponseObject.getString("maxResults"), apiResponseObject.getString("maxResults"));
    Assert.assertEquals(esbResponseObject.getString("startPosition"),
            apiResponseObject.getString("startPosition"));

}

From source file:com.example.klaudia.myapplication.Searcher.java

public Recipe[] getRecipesFromArray(JSONArray array) {
    Recipe[] recipes = null;// w w w .j av a2 s .  c  o m
    try {
        recipes = new Recipe[array.length()];
        for (int i = 0; i < array.length(); i++) {
            Recipe recipe = getRecipeById(array.getJSONObject(i).getInt("id"));
            recipes[i] = recipe;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return recipes;
}

From source file:com.example.klaudia.myapplication.Searcher.java

public HashMap<String, Bitmap> getRecipesTitlesmagesFromArray(JSONArray array) {
    HashMap<String, Bitmap> result = new HashMap<String, Bitmap>();
    try {/*  w w w.  j  av a2  s.  c  o m*/
        for (int i = 0; i < array.length(); i++) {
            JSONObject recipe = array.getJSONObject(i);
            String title = recipe.getString("title");
            Bitmap tmp = new DownloadImageTask().execute(recipe.getString("image")).get();
            Bitmap scaled = Bitmap.createScaledBitmap(tmp, 50, 50, true);
            result.put(title, scaled);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}