JSONArray

public class JSONArray extends Object

A dense indexed sequence of values. Values may be any mix of JSONObjects, other JSONArrays, Strings, Booleans, Integers, Longs, Doubles, null or 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 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 nonfinal, 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.

Public Constructor Summary

JSONArray()
Creates a JSONArray with no values.
JSONArray(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(String json)
Creates a new JSONArray with values from the JSON string.
JSONArray(Object array)
Creates a new JSONArray with values from the given primitive array.

Public Method Summary

boolean
equals(Object o)
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.
String
getString(int index)
Returns the value at index if it exists, coercing it if necessary.
int
boolean
isNull(int index)
Returns true if this array has no value at index, or if its value is the null reference or NULL.
String
join(String separator)
Returns a new string by alternating this array's values with separator.
int
length()
Returns the number of values in this array.
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.
String
optString(int index)
Returns the value at index if it exists, coercing it if necessary.
String
optString(int index, String fallback)
Returns the value at index if it exists, coercing it if necessary.
JSONArray
put(int value)
Appends value to the end of this array.
JSONArray
put(long 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(double value)
Appends value to the end of this array.
JSONArray
put(int index, Object 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, int value)
Sets the value at index to value, null padding this array to the required length if necessary.
JSONArray
put(boolean value)
Appends value to the end of this array.
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(Object value)