/** * Get the optional JSONArray associated with an index. * * @param index * subscript * @return A JSONArray value, or null if the index has no value, or if the * value is not a JSONArray. */ public JSONArray optJSONArray(int index) { Object o = this.opt(index); return o instanceof JSONArray ? (JSONArray) o : null; }
/** * Get the optional JSONObject associated with an index. Null is returned if * the key is not found, or null if the index has no value, or if the value * is not a JSONObject. * * @param index * The index must be between 0 and length() - 1. * @return A JSONObject value. */ public JSONObject optJSONObject(int index) { Object o = this.opt(index); return o instanceof JSONObject ? (JSONObject) o : null; }
/** * Returns the value at {@code index} if it exists and is a {@code * JSONArray}. Returns null otherwise. */ public JSONArray optJSONArray(int index) { Object object = opt(index); return object instanceof JSONArray ? (JSONArray) object : null; }
/** * Returns the value at {@code index} if it exists and is a {@code * JSONObject}. Returns null otherwise. */ public JSONObject optJSONObject(int index) { Object object = opt(index); return object instanceof JSONObject ? (JSONObject) object : null; }
/** * Returns the value at {@code index} if it exists and is a {@code * JSONArray}. Returns null otherwise. * * @param index Which value to get. * @return the value at the specified location. */ public JSONArray optJSONArray(int index) { Object object = opt(index); return object instanceof JSONArray ? (JSONArray) object : null; }
/** * Returns true if this array has no value at {@code index}, or if its value is the {@code null} * reference or {@link JSONObject#NULL}. * * @param index Which value to check. * @return true if the value is null. */ public boolean isNull(int index) { Object value = opt(index); return value == null || value == JSONObject.NULL; }
/** * Returns the value at {@code index} if it exists and is a {@code * JSONObject}. Returns null otherwise. * * @param index Which value to get. * @return the value at the specified location. */ public JSONObject optJSONObject(int index) { Object object = opt(index); return object instanceof JSONObject ? (JSONObject) object : null; }
/** * Returns true if this array has no value at {@code index}, or if its value * is the {@code null} reference or {@link JSONObject#NULL}. */ public boolean isNull(int index) { Object value = opt(index); return value == null || value == JSONObject.NULL; }
/** * Determine if the value is null. * * @param index * The index must be between 0 and length() - 1. * @return true if the value at the index is null, or if there is no value. */ public boolean isNull(int index) { return JSONObject.NULL.equals(this.opt(index)); }
/** * Get the optional string associated with an index. The defaultValue is * returned if the key is not found. * * @param index * The index must be between 0 and length() - 1. * @param defaultValue * The default value. * @return A String value. */ public String optString(int index, String defaultValue) { Object object = this.opt(index); return JSONObject.NULL.equals(object) ? defaultValue : object .toString(); }
@NonNull private static String[] jsonArrayToList(@Nullable JSONArray array) { final List<String> list = new ArrayList<>(); if (array != null) { final int length = array.length(); for (int i = 0; i < length; i++) { list.add(String.valueOf(array.opt(i))); } } return list.toArray(new String[list.size()]); } }
/** * Returns the value at {@code index} if it exists and is a long or * can be coerced to a long. Returns {@code fallback} otherwise. */ public long optLong(int index, long fallback) { Object object = opt(index); Long result = JSON.toLong(object); return result != null ? result : fallback; }
/** * Returns the value at {@code index} if it exists and is a double or can * be coerced to a double. Returns {@code fallback} otherwise. */ public double optDouble(int index, double fallback) { Object object = opt(index); Double result = JSON.toDouble(object); return result != null ? result : fallback; }
/** * Returns the value at {@code index} if it exists and is a boolean or can * be coerced to a boolean. Returns {@code fallback} otherwise. */ public boolean optBoolean(int index, boolean fallback) { Object object = opt(index); Boolean result = JSON.toBoolean(object); return result != null ? result : fallback; }
/** * Returns the value at {@code index} if it exists and is an int or * can be coerced to an int. Returns {@code fallback} otherwise. */ public int optInt(int index, int fallback) { Object object = opt(index); Integer result = JSON.toInteger(object); return result != null ? result : fallback; }
/** * Returns the value at {@code index} if it exists, coercing it if * necessary. Returns {@code fallback} if no such value exists. */ public String optString(int index, String fallback) { Object object = opt(index); String result = JSON.toString(object); return result != null ? result : fallback; }
/** * Returns the value at {@code index} if it exists and is a boolean or can be coerced to a * boolean. Returns {@code fallback} otherwise. * * @param index Which value to get. * @param fallback the fallback value to return if no value exists. * @return the value at the specified location or the fallback value. */ public boolean optBoolean(int index, boolean fallback) { Object object = opt(index); Boolean result = JSON.toBoolean(object); return result != null ? result : fallback; }
/** * Returns the value at {@code index} if it exists and is a long or can be coerced to a long. * Returns {@code fallback} otherwise. * * @param index Which value to get. * @param fallback The fallback value to use if no value is at the specified location. * @return the value at the specified location or the fallback value. */ public long optLong(int index, long fallback) { Object object = opt(index); Long result = JSON.toLong(object); return result != null ? result : fallback; }
/** * Returns the value at {@code index} if it exists, coercing it if necessary. Returns * {@code fallback} if no such value exists. * * @param index Which value to get. * @param fallback The fallback value to use if no value is at the specified location. * @return the value at the specified location or the fallback value. */ public String optString(int index, String fallback) { Object object = opt(index); String result = JSON.toString(object); return result != null ? result : fallback; }
/** * Get the object value associated with an index. * * @param index * The index must be between 0 and length() - 1. * @return An object value. * @throws JSONException * If there is no value for the index. */ public Object get(int index) throws JSONException { Object object = this.opt(index); if (object == null) { throw new JSONException("JSONArray[" + index + "] not found."); } return object; }