Skip to content

Most visited

Recently visited

navigation
Added in API level 1

JSONArray

public class JSONArray
extends Object

java.lang.Object
   ↳ org.json.JSONArray


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.

Summary

Public constructors

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 methods

boolean equals(Object o)

Compares this instance with the specified object and indicates if they are equal.

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 hashCode()

Returns an integer hash code for this object.

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, double fallback)

Returns the value at index if it exists and is a double or can be coerced to a double.

double optDouble(int index)

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, long fallback)

Returns the value at index if it exists and is a long or can be coerced to a long.

long optLong(int index)

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(Object value)

Appends value to the end of this array.

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, boolean 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, Object 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(int value)

Appends value to the end of this array.

JSONArray put(long value)

Appends value to the end of this array.

JSONArray put(double value)

Appends value to the end of this array.

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.

String toString()

Encodes this array as a compact JSON string, such as:

[94043,90210]

String toString(int indentSpaces)

Encodes this array as a human readable JSON string for debugging, such as:

 [
     94043,
     90210
 ]

Inherited methods

From class java.lang.Object

Public constructors

JSONArray

Added in API level 1
JSONArray ()

Creates a JSONArray with no values.

JSONArray

Added in API level 1
JSONArray (Collection copyFrom)

Creates a new JSONArray by copying all values from the given collection.

Parameters
copyFrom Collection: a collection whose values are of supported types. Unsupported values are not permitted and will yield an array in an inconsistent state.

JSONArray

Added in API level 1
JSONArray (JSONTokener readFrom)

Creates a new JSONArray with values from the next array in the tokener.

Parameters
readFrom JSONTokener: a tokener whose nextValue() method will yield a JSONArray.
Throws
JSONException if the parse fails or doesn't yield a JSONArray.

JSONArray

Added in API level 1
JSONArray (String json)

Creates a new JSONArray with values from the JSON string.

Parameters
json String: a JSON-encoded string containing an array.
Throws
JSONException if the parse fails or doesn't yield a JSONArray.

JSONArray

Added in API level 19
JSONArray (Object array)

Creates a new JSONArray with values from the given primitive array.

Parameters
array Object
Throws
JSONException

Public methods

equals

Added in API level 1
boolean equals (Object o)

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.

The default implementation returns true only if this == o. See Writing a correct equals method if you intend implementing your own equals method.

The general contract for the equals and hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or neither of them.

Parameters
o Object: the object to compare this instance with.
Returns
boolean true if the specified object is equal to this Object; false otherwise.

get

Added in API level 1
Object get (int index)

Returns the value at index.

Parameters
index int
Returns
Object
Throws
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.

getBoolean

Added in API level 1
boolean getBoolean (int index)

Returns the value at index if it exists and is a boolean or can be coerced to a boolean.

Parameters
index int
Returns
boolean
Throws
JSONException if the value at index doesn't exist or cannot be coerced to a boolean.

getDouble

Added in API level 1
double getDouble (int index)

Returns the value at index if it exists and is a double or can be coerced to a double.

Parameters
index int
Returns
double
Throws
JSONException if the value at index doesn't exist or cannot be coerced to a double.

getInt

Added in API level 1
int getInt (int index)

Returns the value at index if it exists and is an int or can be coerced to an int.

Parameters
index int
Returns
int
Throws
JSONException if the value at index doesn't exist or cannot be coerced to a int.

getJSONArray

Added in API level 1
JSONArray getJSONArray (int index)

Returns the value at index if it exists and is a JSONArray.

Parameters
index int
Returns
JSONArray
Throws
JSONException if the value doesn't exist or is not a JSONArray.

getJSONObject

Added in API level 1
JSONObject getJSONObject (int index)

Returns the value at index if it exists and is a JSONObject.

Parameters
index int
Returns
JSONObject
Throws
JSONException if the value doesn't exist or is not a JSONObject.

getLong

Added in API level 1
long getLong (int index)

Returns the value at index if it exists and is a long or can be coerced to a long.

Parameters
index int
Returns
long
Throws
JSONException if the value at index doesn't exist or cannot be coerced to a long.

getString

Added in API level 1
String getString (int index)

Returns the value at index if it exists, coercing it if necessary.

Parameters
index int
Returns
String
Throws
JSONException if no such value exists.

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
int this object's hash code.

isNull

Added in API level 1
boolean isNull (int index)

Returns true if this array has no value at index, or if its value is the null reference or NULL.

Parameters
index int
Returns
boolean

join

Added in API level 1
String join (String separator)

Returns a new string by alternating this array's values with 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"

Parameters
separator String
Returns
String
Throws
JSONException

length

Added in API level 1
int length ()

Returns the number of values in this array.

Returns
int

opt

Added in API level 1
Object opt (int index)

Returns the value at index, or null if the array has no value at index.

Parameters
index int
Returns
Object

optBoolean

Added in API level 1
boolean optBoolean (int index)

Returns the value at index if it exists and is a boolean or can be coerced to a boolean. Returns false otherwise.

Parameters
index int
Returns
boolean

optBoolean

Added in API level 1
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. Returns fallback otherwise.

Parameters
index int
fallback boolean
Returns
boolean

optDouble

Added in API level 1
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. Returns fallback otherwise.

Parameters
index int
fallback double
Returns
double

optDouble

Added in API level 1
double optDouble (int index)

Returns the value at index if it exists and is a double or can be coerced to a double. Returns NaN otherwise.

Parameters
index int
Returns
double

optInt

Added in API level 1
int optInt (int index)

Returns the value at index if it exists and is an int or can be coerced to an int. Returns 0 otherwise.

Parameters
index int
Returns
int

optInt

Added in API level 1
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. Returns fallback otherwise.

Parameters
index int
fallback int
Returns
int

optJSONArray

Added in API level 1
JSONArray optJSONArray (int index)

Returns the value at index if it exists and is a JSONArray. Returns null otherwise.

Parameters
index int
Returns
JSONArray

optJSONObject

Added in API level 1
JSONObject optJSONObject (int index)

Returns the value at index if it exists and is a JSONObject. Returns null otherwise.

Parameters
index int
Returns
JSONObject

optLong

Added in API level 1
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. Returns fallback otherwise.

Parameters
index int
fallback long
Returns
long

optLong

Added in API level 1
long optLong (int index)

Returns the value at index if it exists and is a long or can be coerced to a long. Returns 0 otherwise.

Parameters
index int
Returns
long

optString

Added in API level 1
String optString (int index)

Returns the value at index if it exists, coercing it if necessary. Returns the empty string if no such value exists.

Parameters
index int
Returns
String

optString

Added in API level 1
String optString (int index, 
                String fallback)

Returns the value at index if it exists, coercing it if necessary. Returns fallback if no such value exists.

Parameters
index int
fallback String
Returns
String

put

Added in API level 1
JSONArray put (Object value)

Appends value to the end of this array.

Parameters
value Object: a JSONObject, JSONArray, String, Boolean, Integer, Long, Double, 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.
Returns
JSONArray this array.

put

Added in API level 1
JSONArray put (int index, 
                int value)

Sets the value at index to value, null padding this array to the required length if necessary. If a value already exists at index, it will be replaced.

Parameters
index int
value int
Returns
JSONArray this array.
Throws
JSONException

put

Added in API level 1
JSONArray put (int index, 
                boolean value)

Sets the value at index to value, null padding this array to the required length if necessary. If a value already exists at index, it will be replaced.

Parameters
index int
value boolean
Returns
JSONArray this array.
Throws
JSONException

put

Added in API level 1
JSONArray put (int index, 
                long value)

Sets the value at index to value, null padding this array to the required length if necessary. If a value already exists at index, it will be replaced.

Parameters
index int
value long
Returns
JSONArray this array.
Throws
JSONException

put

Added in API level 1
JSONArray put (int index, 
                Object value)

Sets the value at index to value, null padding this array to the required length if necessary. If a value already exists at index, it will be replaced.

Parameters
index int
value Object: a JSONObject, JSONArray, String, Boolean, Integer, Long, Double, NULL, or null. May not be NaNs or infinities.
Returns
JSONArray this array.
Throws
JSONException

put

Added in API level 1
JSONArray put (boolean value)

Appends value to the end of this array.

Parameters
value boolean
Returns
JSONArray this array.

put

Added in API level 1
JSONArray put (int index, 
                double value)

Sets the value at index to value, null padding this array to the required length if necessary. If a value already exists at index, it will be replaced.

Parameters
index int
value double: a finite value. May not be NaNs or infinities.
Returns
JSONArray this array.
Throws
JSONException

put

Added in API level 1
JSONArray put (int value)

Appends value to the end of this array.

Parameters
value int
Returns
JSONArray this array.

put

Added in API level 1
JSONArray put (long value)

Appends value to the end of this array.

Parameters
value long
Returns
JSONArray this array.

put

Added in API level 1
JSONArray put (double value)

Appends value to the end of this array.

Parameters
value double: a finite value. May not be NaNs or infinities.
Returns
JSONArray this array.
Throws
JSONException

remove

Added in API level 19
Object remove (int index)

Removes and returns the value at index, or null if the array has no value at index.

Parameters
index int
Returns
Object

toJSONObject

Added in API level 1
JSONObject toJSONObject (JSONArray names)

Returns a new object whose values are the values in this array, and whose names are the values in 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.

Parameters
names JSONArray
Returns
JSONObject
Throws
JSONException

toString

Added in API level 1
String toString ()

Encodes this array as a compact JSON string, such as:

[94043,90210]

Returns
String a printable representation of this object.

toString

Added in API level 1
String toString (int indentSpaces)

Encodes this array as a human readable JSON string for debugging, such as:

 [
     94043,
     90210
 ]

Parameters
indentSpaces int: the number of spaces to indent for each level of nesting.
Returns
String
Throws
JSONException
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.