Example usage for org.json JSONArray JSONArray

List of usage examples for org.json JSONArray JSONArray

Introduction

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

Prototype

public JSONArray() 

Source Link

Document

Construct an empty JSONArray.

Usage

From source file:com.scvngr.levelup.core.net.request.factory.UserRequestFactoryTest.java

/**
 * Helper to get the String representation of the JSON that we are expecting in the request.
 *
 * @return the json that we are expecting the request to create.
 *//*from  w  w  w  .  ja  va2 s . c  o m*/
@NonNull
private String getExpectedRegisterJsonWithPermissions() {
    final JSONObject object = new JSONObject();
    final JSONObject userObject = new JSONObject();
    final JSONArray permissionObject = new JSONArray();

    try {
        final Context context = NullUtils.nonNullContract(getContext());

        RequestUtils.addApiKeyToRequestBody(context, object);
        userObject.put(UserRequestFactory.PARAM_FIRST_NAME, "first_name");
        userObject.put(UserRequestFactory.PARAM_LAST_NAME, "last_name");
        userObject.put(UserRequestFactory.PARAM_EMAIL, "email@example.com");
        userObject.put(UserRequestFactory.PARAM_TERMS_ACCEPTED, true);

        permissionObject.put("permission1");
        permissionObject.put("permission2");

        object.put(UserRequestFactory.OUTER_PARAM_USER, userObject);
        object.put(UserRequestFactory.OUTER_PARAM_PERMISSION_KEYNAMES, permissionObject);
    } catch (final JSONException e) {
        LogManager.e("JSONException building register request", e);
    }

    return object.toString();
}

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

@Override
public void getScanData(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "getScanData");
    JSONArray jsonDevices = new JSONArray();
    for (BluetoothDevice device : bluetoothDevices) {
        String deviceID = device.getAddress();
        JSONObject jsonDevice = new JSONObject();
        Tools.addProperty(jsonDevice, Tools.DEVICE_ID, deviceID);
        Tools.addProperty(jsonDevice, Tools.DEVICE_NAME, device.getName());
        Tools.addProperty(jsonDevice, Tools.IS_CONNECTED, bluetoothGatt.getConnectedDevices().contains(device));
        Tools.addProperty(jsonDevice, Tools.RSSI, mapRssiData.get(deviceID));
        Tools.addProperty(jsonDevice, Tools.ADVERTISEMENT_DATA,
                Tools.decodeAdvData(mapDeviceAdvData.get(deviceID)));
        jsonDevices.put(jsonDevice);//from   w  w w .  j a v a 2s  .  c om
    }
    callbackContext.success(jsonDevices);
}

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

@Override
public void getConnectedDevices(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "getConnectedDevices");
    if (!isInitialized(callbackContext)) {
        return;/*from  w ww. j a v  a 2s.co  m*/
    }
    @SuppressWarnings("unchecked")
    List<BluetoothDevice> bluetoothDevices = bluetoothGatt.getConnectedDevices();
    JSONArray jsonDevices = new JSONArray();
    for (BluetoothDevice device : bluetoothDevices) {
        JSONObject jsonDevice = new JSONObject();
        Tools.addProperty(jsonDevice, Tools.DEVICE_ID, device.getAddress());
        Tools.addProperty(jsonDevice, Tools.DEVICE_NAME, device.getName());
        jsonDevices.put(jsonDevice);
    }
    callbackContext.success(jsonDevices);
}

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

@Override
public void getPairedDevices(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "getPairedDevices");
    if (!isInitialized(callbackContext)) {
        return;/*  w w w .  j a va 2  s  . c o  m*/
    }
    Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
    JSONArray jsonDevices = new JSONArray();
    for (BluetoothDevice device : bondedDevices) {
        JSONObject jsonDevice = new JSONObject();
        Tools.addProperty(jsonDevice, Tools.DEVICE_ID, device.getAddress());
        Tools.addProperty(jsonDevice, Tools.DEVICE_NAME, device.getName());
        jsonDevices.put(jsonDevice);
    }
    callbackContext.success(jsonDevices);
}

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

@Override
public void getCharacteristics(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "getCharacteristics");
    if (!isInitialized(callbackContext)) {
        return;//w  ww.ja  v a  2  s.  c o  m
    }
    String deviceID = Tools.getData(json, Tools.DEVICE_ID);
    String serviceIndex = Tools.getData(json, Tools.SERVICE_INDEX);
    String[] args = new String[] { deviceID, serviceIndex };
    if (!isNullOrEmpty(args, callbackContext)) {
        return;
    }
    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceID);
    if (!isConnected(device)) {
        Tools.sendErrorMsg(callbackContext);
        return;
    }

    if (serviceIndex == null) {
        Tools.sendErrorMsg(callbackContext);
        return;
    }
    JSONObject jsonObject = new JSONObject();
    Tools.addProperty(jsonObject, Tools.DEVICE_ID, deviceID);
    JSONArray characteristics = new JSONArray();
    int size = getService(deviceID, serviceIndex).getCharacteristics().size();
    for (int i = 0; i < size; i++) {
        BluetoothGattCharacteristic bluetoothGattCharacteristic = getCharacteristic(deviceID, serviceIndex,
                String.valueOf(i));
        UUID charateristicUUID = bluetoothGattCharacteristic.getUuid();
        JSONObject characteristic = new JSONObject();
        Tools.addProperty(characteristic, Tools.CHARACTERISTIC_INDEX, i);
        Tools.addProperty(characteristic, Tools.CHARACTERISTIC_UUID, charateristicUUID);
        Tools.addProperty(characteristic, Tools.CHARACTERISTIC_NAME, Tools.lookup(charateristicUUID));
        Tools.addProperty(characteristic, Tools.CHARACTERISTIC_PROPERTY,
                Tools.decodeProperty(bluetoothGattCharacteristic.getProperties()));
        characteristics.put(characteristic);
    }
    Tools.addProperty(jsonObject, Tools.CHARACTERISTICS, characteristics);
    callbackContext.success(jsonObject);
}

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

@Override
public void getDescriptors(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "getDescriptors");
    if (!isInitialized(callbackContext)) {
        return;//from   w ww.j ava  2s  . c  o m
    }
    String deviceID = Tools.getData(json, Tools.DEVICE_ID);
    String serviceIndex = Tools.getData(json, Tools.SERVICE_INDEX);
    String characteristicIndex = Tools.getData(json, Tools.CHARACTERISTIC_INDEX);
    String[] args = new String[] { deviceID, serviceIndex, characteristicIndex };
    if (!isNullOrEmpty(args, callbackContext)) {
        return;
    }
    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceID);
    if (!isConnected(device)) {
        Tools.sendErrorMsg(callbackContext);
        return;
    }
    JSONObject jsonObject = new JSONObject();
    Tools.addProperty(jsonObject, Tools.DEVICE_ID, deviceID);
    JSONArray descriptors = new JSONArray();
    @SuppressWarnings("unchecked")
    List<BluetoothGattDescriptor> listBluetoothGattDescriptors = getCharacteristic(deviceID, serviceIndex,
            characteristicIndex).getDescriptors();
    int length = listBluetoothGattDescriptors.size();
    for (int i = 0; i < length; i++) {
        UUID uuid = listBluetoothGattDescriptors.get(i).getUuid();
        JSONObject descriptor = new JSONObject();
        Tools.addProperty(descriptor, Tools.DESCRIPTOR_INDEX, i);
        Tools.addProperty(descriptor, Tools.DESCRIPTOR_UUID, uuid);
        Tools.addProperty(descriptor, Tools.DESCRIPTOR_NAME, Tools.lookup(uuid));
        descriptors.put(descriptor);
    }
    Tools.addProperty(jsonObject, Tools.DESCRIPTORS, descriptors);
    callbackContext.success(jsonObject);
}

From source file:com.mikecorrigan.trainscorekeeper.Game.java

public boolean write(final Context context) {
    Log.vc(VERBOSE, TAG, "write: context=" + context);

    // Ignore any undo events.
    List<ScoreEvent> temp = scoreEvents = scoreEvents.subList(0, lastScoreEvent);

    // Convert the list of events to a JSON array, then convert to a string.
    String string = null;/*from w  w w  .j a va  2  s . co  m*/
    try {
        JSONArray jsonScoreEvents = new JSONArray();
        for (ScoreEvent scoreEvent : temp) {
            JSONObject jsonScoreEvent = scoreEvent.toJson();
            if (jsonScoreEvent == null) {
                Log.w(TAG, "write: skipping score event");
                continue;
            }

            jsonScoreEvents.put(jsonScoreEvents.length(), jsonScoreEvent);
        }

        JSONObject jsonRoot = new JSONObject();
        jsonRoot.put(JsonSpec.SCORES_KEY, jsonScoreEvents);
        jsonRoot.put(JsonSpec.GAME_SPEC, spec);

        string = jsonRoot.toString();
    } catch (JSONException e) {
        Log.th(TAG, e, "write: failed");
        return false;
    }

    // Write the string into a file.
    if (string != null) {
        String path = context.getFilesDir() + File.separator + SAVED_FILE;
        File file = new File(path);
        FileUtils.writeStringToFile(file, string);
    }

    return true;
}

From source file:game.objects.PlayerList.java

public JSONArray getPlayersJSON() throws JSONException {
    JSONArray arr = new JSONArray();
    for (int key : players.keySet()) {
        JSONObject json = new JSONObject();
        json.put("PlayerID", "00" + (key + 1));
        json.put("DisplayName", players.get(key).getName());
        json.put("PlayerOrder", key);
        json.put("TroopsToDeploy", players.get(key).getTroopsToDeploy());
        json.put("PlayerImage", players.get(key).getPlayerImage());
        arr.put(json);/* ww  w.java  2 s.c  o  m*/
    }
    return arr;
}

From source file:org.projectbuendia.client.models.Encounter.java

/** Serializes this into a {@link JSONObject}. */
public JSONObject toJson() throws JSONException {
    JSONObject json = new JSONObject();
    json.put(Server.PATIENT_UUID_KEY, patientUuid);
    json.put(Server.ENCOUNTER_TIMESTAMP, timestamp.getMillis() / 1000);
    if (observations.length > 0) {
        JSONArray observationsJson = new JSONArray();
        for (Observation obs : observations) {
            JSONObject observationJson = new JSONObject();
            observationJson.put(Server.OBSERVATION_QUESTION_UUID, obs.conceptUuid);
            String valueKey = obs.type == Observation.Type.DATE ? Server.OBSERVATION_ANSWER_DATE
                    : Server.OBSERVATION_ANSWER_UUID;
            observationJson.put(valueKey, obs.value);
            observationsJson.put(observationJson);
        }/*from   w w  w.j  a  v a  2 s  .  c  o  m*/
        json.put(Server.ENCOUNTER_OBSERVATIONS_KEY, observationsJson);
    }
    if (orderUuids.length > 0) {
        JSONArray orderUuidsJson = new JSONArray();
        for (String orderUuid : orderUuids) {
            orderUuidsJson.put(orderUuid);
        }
        json.put(Server.ENCOUNTER_ORDER_UUIDS, orderUuidsJson);
    }
    return json;
}

From source file:com.riotgames.mondev.JMXDiscovery.java

protected String discoverMBeans(String key) throws Exception {
    try {/*  w w w .  j av  a2 s  .  c  om*/
        HashMap<String, String[]> env = null;
        if (null != username && null != password) {
            env = new HashMap<String, String[]>();
            env.put(JMXConnector.CREDENTIALS, new String[] { username, password });
        }

        jmxc = JMXConnectorFactory.connect(jmxServerUrl, env);
        mbsc = jmxc.getMBeanServerConnection();

        ObjectName filter = new ObjectName(key);
        JSONArray beanList = new JSONArray();
        JSONObject mapping = new JSONObject();

        Set beans = mbsc.queryMBeans(filter, null);
        for (Object obj : beans) {
            JSONObject bean = new JSONObject();
            ObjectName beanName;

            // Return the ObjectName instance correctly for both Objects and Instances
            if (obj instanceof ObjectName)
                beanName = (ObjectName) obj;
            else if (obj instanceof ObjectInstance)
                beanName = ((ObjectInstance) obj).getObjectName();
            else
                throw new RuntimeException("Unexpected object type: " + obj);

            // Build the standing info, description and object path
            MBeanInfo mbi = mbsc.getMBeanInfo(beanName);
            bean.put("{#JMXDESC}", mbi.getDescription());
            bean.put("{#JMXOBJ}", beanName.getCanonicalName());

            // Build a list of all the MBean properties as {#PROP<NAME>}
            Hashtable<String, String> pt = beanName.getKeyPropertyList();
            for (Map.Entry<String, String> prop : pt.entrySet())
                bean.put(String.format("{#PROP%s}", prop.getKey().toUpperCase()), prop.getValue());

            beanList.put(bean);
        }

        mapping.put("data", beanList);
        return mapping.toString();
    } catch (Exception e) {
        JSONArray data = new JSONArray();
        JSONObject out = new JSONObject();
        out.put("data", data);

        return out.toString();
    } finally {
        try {
            if (null != jmxc)
                jmxc.close();
        } catch (java.io.IOException exception) {
        }

        jmxc = null;
        mbsc = null;
    }
}