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(Object array) throws JSONException 

Source Link

Document

Construct a JSONArray from an array

Usage

From source file:com.whizzosoftware.hobson.scheduler.ical.ICalTask.java

public ICalTask(ActionManager actionManager, String providerId, VEvent event, TaskExecutionListener listener)
        throws InvalidVEventException {
    this.actionManager = actionManager;
    this.providerId = providerId;
    this.event = event;
    this.listener = listener;

    if (event != null) {
        // adjust start time if sun offset is set
        Property sunOffset = event.getProperty(PROP_SUN_OFFSET);
        if (sunOffset != null) {
            try {
                solarOffset = new SolarOffset(sunOffset.getValue());
            } catch (ParseException e) {
                throw new InvalidVEventException("Invalid X-SUN-OFFSET", e);
            }// w  w  w. j a v a 2s  . c  o  m
        }

        // parse actions
        Property commentProp = event.getProperty("COMMENT");
        if (commentProp != null) {
            JSONArray arr = new JSONArray(new JSONTokener(commentProp.getValue()));
            for (int i = 0; i < arr.length(); i++) {
                JSONObject json = arr.getJSONObject(i);
                if (json.has("pluginId") && json.has("actionId")) {
                    addActionRef(json);
                } else {
                    throw new InvalidVEventException("Found scheduled event with no plugin and/or action");
                }
            }
        } else {
            throw new InvalidVEventException("ICalEventTask event must have a COMMENT property");
        }
    } else {
        throw new InvalidVEventException("ICalEventTask must have a non-null event");
    }
}

From source file:ai.susi.mind.SusiSkill.java

public JSONObject toJSON() {
    JSONObject json = new JSONObject(true);
    json.put("id", id);
    if (this.keys != null && this.keys.size() > 0)
        json.put("keys", new JSONArray(this.keys));
    JSONArray p = new JSONArray();
    this.phrases.forEach(phrase -> p.put(phrase.toJSON()));
    json.put("phrases", p);
    JSONArray i = new JSONArray();
    this.inferences.forEach(inference -> i.put(inference.getJSON()));
    json.put("process", i);
    JSONArray a = new JSONArray();
    this.actions.forEach(action -> a.put(action.toJSONClone()));
    json.put("actions", a);
    if (this.comment != null && this.comment.length() > 0)
        json.put("comment", comment);
    if (this.score != null)
        json.put("score", score.score);
    return json;// ww w . j a  v a 2 s . co m
}

From source file:nl.hnogames.domoticzapi.Parsers.DevicesParser.java

@Override
public void parseResult(String result) {

    try {/*w w w . jav a 2  s .co  m*/
        JSONArray jsonArray = new JSONArray(result);
        ArrayList<DevicesInfo> mDevices = new ArrayList<>();

        if (jsonArray.length() > 0) {

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject row = jsonArray.getJSONObject(i);
                mDevices.add(new DevicesInfo(row));
            }
        }

        if (idx == 999999)
            receiver.onReceiveDevices(mDevices);
        else {
            receiver.onReceiveDevice(getDevice(idx, mDevices, scene_or_group));
        }
    } catch (JSONException e) {
        Log.e(TAG, "DevicesParser JSON exception");
        e.printStackTrace();
        receiver.onError(e);
    }
}

From source file:com.markupartist.sthlmtraveling.provider.planner.JourneyQuery.java

public JSONObject toJson(boolean all) throws JSONException {
    JSONObject jsonOrigin = new JSONObject();
    jsonOrigin.put("id", origin.id);
    jsonOrigin.put("name", origin.name);
    jsonOrigin.put("latitude", origin.latitude);
    jsonOrigin.put("longitude", origin.longitude);

    if (!all && origin.isMyLocation()) {
        jsonOrigin.put("latitude", 0);
        jsonOrigin.put("longitude", 0);
    }//from   www . j a va2 s  .c om

    JSONObject jsonDestination = new JSONObject();
    jsonDestination.put("id", destination.id);
    jsonDestination.put("name", destination.name);
    jsonDestination.put("latitude", destination.latitude);
    jsonDestination.put("longitude", destination.longitude);

    if (!all && destination.isMyLocation()) {
        jsonDestination.put("latitude", 0);
        jsonDestination.put("longitude", 0);
    }

    JSONObject jsonQuery = new JSONObject();
    if (via != null) {
        JSONObject jsonVia = new JSONObject();
        jsonVia.put("id", via.id);
        jsonVia.put("name", via.name);

        jsonQuery.put("via", jsonVia);
    }

    if (transportModes != null) {
        jsonQuery.put("transportModes", new JSONArray(transportModes));
    }

    jsonQuery.put("alternativeStops", alternativeStops);
    jsonQuery.put("origin", jsonOrigin);
    jsonQuery.put("destination", jsonDestination);

    if (all) {
        jsonQuery.put("ident", ident);
        jsonQuery.put("seqnr", seqnr);
        jsonQuery.put("time", time.format("%F %R"));
        jsonQuery.put("isTimeDeparture", this.isTimeDeparture);
    }

    return jsonQuery;
}

From source file:com.xee.demo.utils.JsonLogger.java

/**
 * Get a json to format/* ww w  . ja v  a2s.  c om*/
 * @param message the json to format
 * @return the pretty json
 */
public static String getPrettyLog(String message) {
    String json;
    try {
        if (message.startsWith("{")) {
            JSONObject jsonObject = new JSONObject(message);
            json = jsonObject.toString(JSON_INDENT);
        } else if (message.startsWith("[")) {
            JSONArray jsonArray = new JSONArray(message);
            json = jsonArray.toString(JSON_INDENT);
        } else {
            json = message;
        }
    } catch (JSONException e) {
        json = message;
    }

    return json;
}

From source file:org.chromium.ChromeBluetooth.java

private JSONObject getBasicDeviceInfo(BluetoothDevice device) throws JSONException {
    JSONObject deviceInfo = new JSONObject();
    deviceInfo.put("address", device.getAddress());
    deviceInfo.put("name", device.getName());
    deviceInfo.put("deviceClass", device.getBluetoothClass().getDeviceClass());
    deviceInfo.put("paired", device.getBondState() == BluetoothDevice.BOND_BONDED);
    deviceInfo.put("connected", isConnected(device));
    deviceInfo.put("uuids", new JSONArray(getUuidStringsFromDevice(device)));
    return deviceInfo;
}

From source file:org.chromium.ChromeBluetooth.java

private JSONObject getLeDeviceInfo(ScanResult leScanResult) throws JSONException {
    JSONObject deviceInfo = getBasicDeviceInfo(leScanResult.getDevice());
    Set<String> uuidStrings = new HashSet<String>();
    uuidStrings.addAll(getUuidStringsFromDevice(leScanResult.getDevice()));
    uuidStrings.addAll(getUuidStringsFromLeScanRecord(leScanResult.getScanRecord()));

    JSONArray uuids = new JSONArray(uuidStrings);

    if (!uuidStrings.isEmpty()) {
        deviceInfo.put("uuids", uuids);
    }// w ww. j  a  v a  2s. com

    deviceInfo.put("rssi", leScanResult.getRssi());
    deviceInfo.put("tx_power", leScanResult.getScanRecord().getTxPowerLevel());

    Iterator serviceDataIt = leScanResult.getScanRecord().getServiceData().entrySet().iterator();
    if (serviceDataIt.hasNext()) {
        Map.Entry<ParcelUuid, byte[]> serviceDataPair = (Map.Entry<ParcelUuid, byte[]>) serviceDataIt.next();
        deviceInfo.put("serviceDataUuid", serviceDataPair.getKey().toString());
        deviceInfo.put("serviceData", Base64.encodeToString(serviceDataPair.getValue(), Base64.NO_WRAP));
    }

    return deviceInfo;
}

From source file:com.tweetlanes.android.App.java

public void updateTwitterAccountCount() {

    mAccounts.clear();//from w  ww . jav  a  2s  . c  o  m

    long currentAccountId = mPreferences.getLong(SHARED_PREFERENCES_KEY_CURRENT_ACCOUNT_ID, -1);
    String accountIndices = mPreferences.getString(SHARED_PREFERENCES_KEY_ACCOUNT_INDICES, null);
    if (accountIndices != null) {
        try {
            JSONArray jsonArray = new JSONArray(accountIndices);
            for (int i = 0; i < jsonArray.length(); i++) {
                Long id = jsonArray.getLong(i);

                String key = getAccountDescriptorKey(id);
                String jsonAsString = mPreferences.getString(key, null);
                if (jsonAsString != null) {
                    AccountDescriptor account = new AccountDescriptor(this, jsonAsString);
                    mAccounts.add(account);

                    if (currentAccountId != -1 && account.getId() == currentAccountId) {
                        mCurrentAccountIndex = i;
                    }
                }
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    if (mCurrentAccountIndex == null && mAccounts.size() > 0) {
        mCurrentAccountIndex = 0;
    }
}

From source file:edu.umass.cs.gigapaxos.paxospackets.FindReplicaGroupPacket.java

@Override
public JSONObject toJSONObjectImpl() throws JSONException {
    JSONObject json = new JSONObject();
    json.put(PaxosPacket.NodeIDKeys.SNDR.toString(), this.nodeID);
    if (this.group != null && this.group.length > 0) {
        JSONArray jsonGroup = new JSONArray(Util.arrayToIntSet(group));
        json.put(PaxosPacket.NodeIDKeys.GROUP.toString(), jsonGroup);
    }//www. ja  v  a  2s .c o m
    return json;
}

From source file:cn.com.lowe.android.tools.net.response.JsonHttpResponseHandler.java

private Object parseResponse(String content) throws JSONException {
    if (content.startsWith("[") && content.endsWith("]")) {
        return new JSONArray(content);
    } else if (content.startsWith("{") && content.endsWith("}")) {
        return new JSONObject(content);
    } else {/*from  w w  w  .j  a  v  a2 s.c o  m*/
        throw new JSONException("response is not json");
    }
}