public class JSONArray
extends java.lang.Object
JSONObjects,
other JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or
JSONObject.NULL. Values may not be NaNs,
infinities, or of any type not listed here.
JSONArray has the same type coercion behavior and optional/mandatory accessors as
JSONObject. See that class' documentation for details.
Warning: this class represents null in two incompatible ways: the standard Java
null reference, and the sentinel value JSONObject.NULL. In particular,
get fails if the requested index holds the null reference, but succeeds if it holds
JSONObject.NULL.
Instances of this class are not thread safe. Although this class is non-final, it was not
designed for inheritance and should not be subclassed. In particular, self-use by overridable
methods is not specified. See Effective Java Item 17, "Design and Document or inheritance
or else prohibit it" for further information.| Constructor and Description |
|---|
JSONArray()
Creates a
JSONArray with no values. |
JSONArray(java.util.Collection<?> copyFrom)
Creates a new
JSONArray by copying all values from the given collection. |
JSONArray(JSONTokener readFrom)
Creates a new
JSONArray with values from the next array in the tokener. |
JSONArray(java.lang.Object array)
Creates a new
JSONArray with values from the given primitive array. |
JSONArray(java.lang.String json)
Creates a new
JSONArray with values from the JSON string. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(java.lang.Object o) |
java.lang.Object |
get(int index)
Returns the value at
index. |
boolean |
getBoolean(int index)
Returns the value at
index if it exists and is a boolean or can be coerced to a
boolean. |
double |
getDouble(int index)
Returns the value at
index if it exists and is a double or can be coerced to a double. |
int |
getInt(int index)
Returns the value at
index if it exists and is an int or can be coerced to an int. |
JSONArray |
getJSONArray(int index)
Returns the value at
index if it exists and is a JSONArray. |
JSONObject |
getJSONObject(int index)
Returns the value at
index if it exists and is a JSONObject. |
long |
getLong(int index)
Returns the value at
index if it exists and is a long or can be coerced to a long. |
java.lang.String |
getString(int index)
Returns the value at
index if it exists, coercing it if necessary. |
int |
hashCode() |
boolean |
isNull(int index)
Returns true if this array has no value at
index, or if its value is the null
reference or JSONObject.NULL. |
java.lang.String |
join(java.lang.String separator)
Returns a new string by alternating this array's values with
separator. |
int |
length() |
java.lang.Object |
opt(int index)
Returns the value at
index, or null if the array has no value at index. |
boolean |
optBoolean(int index)
Returns the value at
index if it exists and is a boolean or can be coerced to a
boolean. |
boolean |
optBoolean(int index,
boolean fallback)
Returns the value at
index if it exists and is a boolean or can be coerced to a
boolean. |
double |
optDouble(int index)
Returns the value at
index if it exists and is a double or can be coerced to a double. |
double |
optDouble(int index,
double fallback)
Returns the value at
index if it exists and is a double or can be coerced to a double. |
int |
optInt(int index)
Returns the value at
index if it exists and is an int or can be coerced to an int. |
int |
optInt(int index,
int fallback)
Returns the value at
index if it exists and is an int or can be coerced to an int. |
JSONArray |
optJSONArray(int index)
Returns the value at
index if it exists and is a JSONArray. |
JSONObject |
optJSONObject(int index)
Returns the value at
index if it exists and is a JSONObject. |
long |
optLong(int index)
Returns the value at
index if it exists and is a long or can be coerced to a long. |
long |
optLong(int index,
long fallback)
Returns the value at
index if it exists and is a long or can be coerced to a long. |
java.lang.String |
optString(int index)
Returns the value at
index if it exists, coercing it if necessary. |
java.lang.String |
optString(int index,
java.lang.String fallback)
Returns the value at
index if it exists, coercing it if necessary. |
JSONArray |
put(boolean value)
Appends
value to the end of this array. |
JSONArray |
put(java.util.Collection<?> value)
Appends
value wrapped by JSONArray to the end of this array. |
JSONArray |
put(double value)
Appends
value to the end of this array. |
JSONArray |
put(int value)
Appends
value to the end of this array. |
JSONArray |
put(int index,
boolean value)
Sets the value at
index to value, null padding this array to the required
length if necessary. |
JSONArray |
put(int index,
java.util.Collection<?> value)
Sets the value at
index to value wrapped into JSONArray, null padding
this array to the required length if necessary. |
JSONArray |
put(int index,
double value)
Sets the value at
index to value, null padding this array to the required
length if necessary. |
JSONArray |
put(int index,
int value)
Sets the value at
index to value, null padding this array to the required
length if necessary. |
JSONArray |
put(int index,
long value)
Sets the value at
index to value, null padding this array to the required
length if necessary. |
JSONArray |
put(int index,
java.lang.Object value)
Sets the value at
index to value, null padding this array to the required
length if necessary. |
JSONArray |
put(long value)
Appends
value to the end of this array. |
JSONArray |
put(java.lang.Object value)
Appends
value to the end of this array. |
java.lang.Object |
remove(int index)
Removes and returns the value at
index, or null if the array has no value at
index. |
JSONObject |
toJSONObject(JSONArray names)
Returns a new object whose values are the values in this array, and whose names are the values
in
names. |
java.lang.String |
toString()
Encodes this array as a compact JSON string, such as:
|
java.lang.String |
toString(int indentSpaces)
Encodes this array as a human readable JSON string for debugging, such as:
|
public JSONArray()
JSONArray with no values.public JSONArray(java.util.Collection<?> copyFrom)
JSONArray by copying all values from the given collection.copyFrom - a collection whose values are of supported types. Unsupported values are not
permitted and will yield an array in an inconsistent state.public JSONArray(JSONTokener readFrom) throws JSONException
JSONArray with values from the next array in the tokener.readFrom - a tokener whose nextValue() method will yield a JSONArray.JSONException - if the parse fails or doesn't yield a JSONArray.public JSONArray(java.lang.String json)
throws JSONException
JSONArray with values from the JSON string.json - a JSON-encoded string containing an array.JSONException - if the parse fails or doesn't yield a JSONArray.public JSONArray(java.lang.Object array)
throws JSONException
JSONArray with values from the given primitive array.array - The values to use.JSONException - if any of the values are non-finite double values (i.e. NaN or infinite)public int length()
public JSONArray put(boolean value)
value to the end of this array.value - The value to append.public JSONArray put(double value) throws JSONException
value to the end of this array.value - a finite value. May not be NaNs or
infinities.JSONException - If the value is unacceptable.public JSONArray put(int value)
value to the end of this array.value - The value to append.public JSONArray put(long value)
value to the end of this array.value - The value to append.public JSONArray put(java.util.Collection<?> value)
value wrapped by JSONArray to the end of this array.value - any collection.public JSONArray put(java.lang.Object value)
value to the end of this array.value - a JSONObject, JSONArray, String, Boolean, Integer, Long, Double,
JSONObject.NULL, or null. May not be NaNs or
infinities. Unsupported values are not permitted and will
cause the array to be in an inconsistent state.public JSONArray put(int index, boolean value) throws JSONException
index to value, null padding this array to the required
length if necessary. If a value already exists at index, it will be replaced.index - Where to put the value.value - The value to set.JSONException - This should never happen.public JSONArray put(int index, double value) throws JSONException
index to value, null padding this array to the required
length if necessary. If a value already exists at index, it will be replaced.index - Where to put the value.value - a finite value. May not be NaNs or
infinities.JSONException - If the value is not a finite value.public JSONArray put(int index, int value) throws JSONException
index to value, null padding this array to the required
length if necessary. If a value already exists at index, it will be replaced.index - Where to put the value.value - The value to set.JSONException - Should never actually happen.public JSONArray put(int index, long value) throws JSONException
index to value, null padding this array to the required
length if necessary. If a value already exists at index, it will be replaced.index - Where to put the value.value - The value to set.JSONException - Should never actually happen.public JSONArray put(int index, java.util.Collection<?> value) throws JSONException
index to value wrapped into JSONArray, null padding
this array to the required length if necessary. If a value already exists at index, it
will be replaced.index - Where to put the value.value - The value to set.JSONException - Should never actually happen.public JSONArray put(int index, java.lang.Object value) throws JSONException
index to value, null padding this array to the required
length if necessary. If a value already exists at index, it will be replaced.index - Where to put the value.value - a JSONObject, JSONArray, String, Boolean, Integer, Long, Double,
JSONObject.NULL, or null. May not be NaNs or
infinities.JSONException - If the value cannot be represented as a finite double value.public boolean isNull(int index)
index, or if its value is the null
reference or JSONObject.NULL.index - Which value to check.public java.lang.Object get(int index)
throws JSONException
index.index - Which value to get.JSONException - if this array has no value at index, or if that value is the
null reference. This method returns normally if the value is
JSONObject#NULL.public java.lang.Object opt(int index)
index, or null if the array has no value at index.index - Which value to get.public java.lang.Object remove(int index)
index, or null if the array has no value at
index.index - Which value to remove.public boolean getBoolean(int index)
throws JSONException
index if it exists and is a boolean or can be coerced to a
boolean.index - Which value to get.JSONException - if the value at index doesn't exist or cannot be coerced to a
boolean.public boolean optBoolean(int index)
index if it exists and is a boolean or can be coerced to a
boolean. Returns false otherwise.index - Which value to get.public boolean optBoolean(int index,
boolean fallback)
index if it exists and is a boolean or can be coerced to a
boolean. Returns fallback otherwise.index - Which value to get.fallback - the fallback value to return if no value exists.public double getDouble(int index)
throws JSONException
index if it exists and is a double or can be coerced to a double.index - Which value to get.JSONException - if the value at index doesn't exist or cannot be coerced to a
double.public double optDouble(int index)
index if it exists and is a double or can be coerced to a double.
Returns NaN otherwise.index - Which value to get.public double optDouble(int index,
double fallback)
index if it exists and is a double or can be coerced to a double.
Returns fallback otherwise.index - Which value to get.fallback - The fallback value to use if no value is at the specified location.public int getInt(int index)
throws JSONException
index if it exists and is an int or can be coerced to an int.index - Which value to get.JSONException - if the value at index doesn't exist or cannot be coerced to a
int.public int optInt(int index)
index if it exists and is an int or can be coerced to an int.
Returns 0 otherwise.index - Which value to get.public int optInt(int index,
int fallback)
index if it exists and is an int or can be coerced to an int.
Returns fallback otherwise.index - Which value to get.fallback - The fallback value to use if no value is at the specified location.public long getLong(int index)
throws JSONException
index if it exists and is a long or can be coerced to a long.index - Which value to get.JSONException - if the value at index doesn't exist or cannot be coerced to a
long.public long optLong(int index)
index if it exists and is a long or can be coerced to a long.
Returns 0 otherwise.index - Which value to get.public long optLong(int index,
long fallback)
index if it exists and is a long or can be coerced to a long.
Returns fallback otherwise.index - Which value to get.fallback - The fallback value to use if no value is at the specified location.public java.lang.String getString(int index)
throws JSONException
index if it exists, coercing it if necessary.index - Which value to get.JSONException - if no such value exists.public java.lang.String optString(int index)
index if it exists, coercing it if necessary. Returns the empty
string if no such value exists.index - Which value to get.public java.lang.String optString(int index,
java.lang.String fallback)
index if it exists, coercing it if necessary. Returns
fallback if no such value exists.index - Which value to get.fallback - The fallback value to use if no value is at the specified location.public JSONArray getJSONArray(int index) throws JSONException
index if it exists and is a JSONArray.index - Which value to get.JSONException - if the value doesn't exist or is not a JSONArray.public JSONArray optJSONArray(int index)
index if it exists and is a JSONArray. Returns null otherwise.index - Which value to get.public JSONObject getJSONObject(int index) throws JSONException
index if it exists and is a JSONObject.index - Which value to get.JSONException - if the value doesn't exist or is not a JSONObject.public JSONObject optJSONObject(int index)
index if it exists and is a JSONObject. Returns null otherwise.index - Which value to get.public JSONObject toJSONObject(JSONArray names) throws JSONException
names. Names and values are paired up by index from 0 through to the shorter array's
length. Names that are not strings will be coerced to strings. This method returns null if
either array is empty.names - The names to apply to the returned values.JSONException - Should not be possible.public java.lang.String join(java.lang.String separator)
throws JSONException
separator. This array's string values are quoted and have their special characters escaped.
For example, the array containing the strings '12" pizza', 'taco' and 'soda' joined on '+'
returns this:
"12\" pizza" + "taco" + "soda"
separator - The string used to separate the returned values.JSONException - Only if there is a coding error.public java.lang.String toString()
[94043,90210]
toString in class java.lang.Objectpublic java.lang.String toString(int indentSpaces)
throws JSONException
[
94043,
90210
]
indentSpaces - the number of spaces to indent for each level of nesting.JSONException - Only if there is a coding error.public boolean equals(java.lang.Object o)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Object