Example usage for org.json JSONArray getLong

List of usage examples for org.json JSONArray getLong

Introduction

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

Prototype

public long getLong(int index) throws JSONException 

Source Link

Document

Get the long value associated with an index.

Usage

From source file:com.trk.aboutme.facebook.SharedPreferencesTokenCachingStrategy.java

private void deserializeKey(String key, Bundle bundle) throws JSONException {
    String jsonString = cache.getString(key, "{}");
    JSONObject json = new JSONObject(jsonString);

    String valueType = json.getString(JSON_VALUE_TYPE);

    if (valueType.equals(TYPE_BOOLEAN)) {
        bundle.putBoolean(key, json.getBoolean(JSON_VALUE));
    } else if (valueType.equals(TYPE_BOOLEAN_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        boolean[] array = new boolean[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getBoolean(i);
        }/*ww  w .  j av a 2 s.co  m*/
        bundle.putBooleanArray(key, array);
    } else if (valueType.equals(TYPE_BYTE)) {
        bundle.putByte(key, (byte) json.getInt(JSON_VALUE));
    } else if (valueType.equals(TYPE_BYTE_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        byte[] array = new byte[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = (byte) jsonArray.getInt(i);
        }
        bundle.putByteArray(key, array);
    } else if (valueType.equals(TYPE_SHORT)) {
        bundle.putShort(key, (short) json.getInt(JSON_VALUE));
    } else if (valueType.equals(TYPE_SHORT_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        short[] array = new short[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = (short) jsonArray.getInt(i);
        }
        bundle.putShortArray(key, array);
    } else if (valueType.equals(TYPE_INTEGER)) {
        bundle.putInt(key, json.getInt(JSON_VALUE));
    } else if (valueType.equals(TYPE_INTEGER_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        int[] array = new int[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getInt(i);
        }
        bundle.putIntArray(key, array);
    } else if (valueType.equals(TYPE_LONG)) {
        bundle.putLong(key, json.getLong(JSON_VALUE));
    } else if (valueType.equals(TYPE_LONG_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        long[] array = new long[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getLong(i);
        }
        bundle.putLongArray(key, array);
    } else if (valueType.equals(TYPE_FLOAT)) {
        bundle.putFloat(key, (float) json.getDouble(JSON_VALUE));
    } else if (valueType.equals(TYPE_FLOAT_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        float[] array = new float[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = (float) jsonArray.getDouble(i);
        }
        bundle.putFloatArray(key, array);
    } else if (valueType.equals(TYPE_DOUBLE)) {
        bundle.putDouble(key, json.getDouble(JSON_VALUE));
    } else if (valueType.equals(TYPE_DOUBLE_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        double[] array = new double[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = jsonArray.getDouble(i);
        }
        bundle.putDoubleArray(key, array);
    } else if (valueType.equals(TYPE_CHAR)) {
        String charString = json.getString(JSON_VALUE);
        if (charString != null && charString.length() == 1) {
            bundle.putChar(key, charString.charAt(0));
        }
    } else if (valueType.equals(TYPE_CHAR_ARRAY)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        char[] array = new char[jsonArray.length()];
        for (int i = 0; i < array.length; i++) {
            String charString = jsonArray.getString(i);
            if (charString != null && charString.length() == 1) {
                array[i] = charString.charAt(0);
            }
        }
        bundle.putCharArray(key, array);
    } else if (valueType.equals(TYPE_STRING)) {
        bundle.putString(key, json.getString(JSON_VALUE));
    } else if (valueType.equals(TYPE_STRING_LIST)) {
        JSONArray jsonArray = json.getJSONArray(JSON_VALUE);
        int numStrings = jsonArray.length();
        ArrayList<String> stringList = new ArrayList<String>(numStrings);
        for (int i = 0; i < numStrings; i++) {
            Object jsonStringValue = jsonArray.get(i);
            stringList.add(i, jsonStringValue == JSONObject.NULL ? null : (String) jsonStringValue);
        }
        bundle.putStringArrayList(key, stringList);
    } else if (valueType.equals(TYPE_ENUM)) {
        try {
            String enumType = json.getString(JSON_VALUE_ENUM_TYPE);
            @SuppressWarnings({ "unchecked", "rawtypes" })
            Class<? extends Enum> enumClass = (Class<? extends Enum>) Class.forName(enumType);
            @SuppressWarnings("unchecked")
            Enum<?> enumValue = Enum.valueOf(enumClass, json.getString(JSON_VALUE));
            bundle.putSerializable(key, enumValue);
        } catch (ClassNotFoundException e) {
        } catch (IllegalArgumentException e) {
        }
    }
}

From source file:br.ufc.mdcc.mpos.net.profile.model.Network.java

public static long[] StringToLongArray(String vlrArray) throws JSONException {
    JSONArray jsonArray = new JSONArray(vlrArray);
    long array[] = new long[jsonArray.length()];

    for (int i = 0; i < jsonArray.length(); i++) {
        array[i] = jsonArray.getLong(i);
    }// www . j av  a  2 s.  c  o  m

    return array;
}

From source file:com.nginious.http.serialize.JsonDeserializer.java

/**
 * Deserializes property with the specified name in the specified json object into a long array.
 * //from w  w  w .j  a v a 2 s. c  o  m
 * @param object the json object
 * @param name the property name
 * @return the deserialized array or <code>null</code> if property doesn't exist
 * @throws SerializerException if unable to deserialize value
 */
protected long[] deserializeLongArray(JSONObject object, String name) throws SerializerException {
    try {
        if (object.has(name)) {
            JSONArray array = object.getJSONArray(name);
            long[] outArray = new long[array.length()];

            for (int i = 0; i < array.length(); i++) {
                outArray[i] = array.getLong(i);
            }

            return outArray;
        }

        return null;
    } catch (JSONException e) {
        throw new SerializerException("Can't deserialize long array property " + name, e);
    }
}

From source file:jorge.tolentino.empty.Empty.java

/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArray of arguments for the plugin.
 * @param callbackContext   The callback context used when calling back into JavaScript.
 * @return                  True when the action was valid, false otherwise.
 *///from  ww w  . ja va 2s .co  m
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (action.equals("vibrate")) {
        this.vibrate(args.getLong(0));
    } else if (action.equals("vibrateWithPattern")) {
        JSONArray pattern = args.getJSONArray(0);
        int repeat = args.getInt(1);
        long[] patternArray = new long[pattern.length()];
        for (int i = 0; i < pattern.length(); i++) {
            patternArray[i] = pattern.getLong(i);
        }
        this.vibrateWithPattern(patternArray, repeat);
    } else if (action.equals("cancelVibration")) {
        this.cancelVibration();
    } else {
        return false;
    }

    // Only alert and confirm are async.
    callbackContext.success();

    return true;
}

From source file:com.liferay.mobile.android.v7.commentmanagerjsonws.CommentmanagerjsonwsService.java

public Long addComment(long groupId, String className, long classPK, String body) throws Exception {
    JSONObject _command = new JSONObject();

    try {//from   w w  w. j  a v a2 s  . co m
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("className", checkNull(className));
        _params.put("classPK", classPK);
        _params.put("body", checkNull(body));

        _command.put("/comment.commentmanagerjsonws/add-comment", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getLong(0);
}

From source file:com.liferay.mobile.android.v7.commentmanagerjsonws.CommentmanagerjsonwsService.java

public Long updateComment(String className, long classPK, long commentId, String subject, String body)
        throws Exception {
    JSONObject _command = new JSONObject();

    try {/*from   w  ww  . ja  va 2  s .  c  o m*/
        JSONObject _params = new JSONObject();

        _params.put("className", checkNull(className));
        _params.put("classPK", classPK);
        _params.put("commentId", commentId);
        _params.put("subject", checkNull(subject));
        _params.put("body", checkNull(body));

        _command.put("/comment.commentmanagerjsonws/update-comment", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getLong(0);
}

From source file:org.apache.cordova.runningapps.RunningApps.java

/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArray of arguments for the plugin.
 * @param callbackContext   The callback context used when calling back into JavaScript.
 * @return                  True when the action was valid, false otherwise.
 */// ww  w  .j a va  2  s.c  o m
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (action.equals("vibrate")) {
        this.vibrate(args.getLong(0));
    } else {
        return false;
    }

    // Only alert and confirm are async.
    callbackContext.success();
    return true;
}

From source file:com.liferay.mobile.android.v7.organization.OrganizationService.java

public Long getOrganizationId(long companyId, String name) throws Exception {
    JSONObject _command = new JSONObject();

    try {/* w w w.j a  v  a  2s .c  o m*/
        JSONObject _params = new JSONObject();

        _params.put("companyId", companyId);
        _params.put("name", checkNull(name));

        _command.put("/organization/get-organization-id", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getLong(0);
}

From source file:com.phonegap.CompassListener.java

/**
 * Executes the request and returns PluginResult.
 * // w  ww  . ja  va2 s  .  c  o m
 * @param action       The action to execute.
 * @param args          JSONArry of arguments for the plugin.
 * @param callbackId   The callback id used when calling back into JavaScript.
 * @return             A PluginResult object with a status and message.
 */
public PluginResult execute(String action, JSONArray args, String callbackId) {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";

    try {
        if (action.equals("start")) {
            this.start();
        } else if (action.equals("stop")) {
            this.stop();
        } else if (action.equals("getStatus")) {
            int i = this.getStatus();
            return new PluginResult(status, i);
        } else if (action.equals("getHeading")) {
            // If not running, then this is an async call, so don't worry about waiting
            if (this.status != RUNNING) {
                int r = this.start();
                if (r == ERROR_FAILED_TO_START) {
                    return new PluginResult(PluginResult.Status.IO_EXCEPTION, ERROR_FAILED_TO_START);
                }
                // Wait until running
                long timeout = 2000;
                while ((this.status == STARTING) && (timeout > 0)) {
                    timeout = timeout - 100;
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (timeout == 0) {
                    return new PluginResult(PluginResult.Status.IO_EXCEPTION,
                            AccelListener.ERROR_FAILED_TO_START);
                }
            }
            float f = this.getHeading();
            return new PluginResult(status, f);
        } else if (action.equals("setTimeout")) {
            this.setTimeout(args.getLong(0));
        } else if (action.equals("getTimeout")) {
            long l = this.getTimeout();
            return new PluginResult(status, l);
        }
        return new PluginResult(status, result);
    } catch (JSONException e) {
        e.printStackTrace();
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }
}

From source file:com.bangz.shotrecorder.SplitManager.java

public SplitManager rebuildFromJSONString(String string) {

    clear();/*from  www  .j a v a 2 s .c om*/

    int i = 0;

    try {
        JSONArray jsonArray = new JSONArray(string);
        while (true) {
            long t = jsonArray.getLong(i++);
            append(t);
        }

    } catch (JSONException e) {
    }

    return this;
}