Expand Minimize Picture-in-picture Power Device Status Voice Recognition Skip Back Skip Forward Minus Plus Play Search
Documentation
Sidebar

Class JSONArray

Hierarchy

java.lang.Object

Package
org.json

Overview

A dense indexed sequence of values. Values may be any mix of {@link JSONObject JSONObjects}, other {@link JSONArray JSONArrays}, Strings, Booleans, Integers, Longs, Doubles, {@code null} or {@link JSONObject#NULL}. Values may not be {@link Double#isNaN() NaNs}, {@link Double#isInfinite() infinities}, or of any type not listed here.

{@code 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 {@code null} reference, and the sentinel value {@link JSONObject#NULL}. In particular, {@code get} fails if the requested index holds the null reference, but succeeds if it holds {@code 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.

Constructors

JSONArray()

Creates a {@code JSONArray} with no values.

public JSONArray()

JSONArray( java.util.Collection copyFrom )

Creates a new {@code JSONArray} by copying all values from the given collection.

public JSONArray(java.util.Collection copyFrom)

Parameters

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

JSONArray( JSONTokener readFrom )

Creates a new {@code JSONArray} with values from the next array in the tokener.

public JSONArray(JSONTokener readFrom)

Parameters

readFrom
a tokener whose nextValue() method will yield a {@code JSONArray}.

JSONArray( String json )

Creates a new {@code JSONArray} with values from the JSON string.

public JSONArray(String json)

Parameters

json
a JSON-encoded string containing an array.

JSONArray( Object array )

Creates a new {@code JSONArray} with values from the given primitive array.

public JSONArray(Object array)

Parameters

array

Methods

length()

Returns the number of values in this array.

public int length()

put( boolean value )

Appends {@code value} to the end of this array.

public org.json.JSONArray put(boolean value)

Parameters

value

put( double value )

Appends {@code value} to the end of this array.

public org.json.JSONArray put(double value) throws org.json.JSONException

Parameters

value
a finite value. May not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.

Throws

put( int value )

Appends {@code value} to the end of this array.

public org.json.JSONArray put(int value)

Parameters

value

put( long value )

Appends {@code value} to the end of this array.

public org.json.JSONArray put(long value)

Parameters

value

put( Object value )

Appends {@code value} to the end of this array.

public org.json.JSONArray put(Object value)

Parameters

value
a {@link JSONObject}, {@link JSONArray}, String, Boolean, Integer, Long, Double, {@link JSONObject#NULL}, or {@code null}. May not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}. Unsupported values are not permitted and will cause the array to be in an inconsistent state.

put( int index, boolean value )

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

public org.json.JSONArray put(int index,boolean value) throws org.json.JSONException

Parameters

index
value

Throws

put( int index, double value )

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

public org.json.JSONArray put(int index,double value) throws org.json.JSONException

Parameters

index
value
a finite value. May not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.

Throws

put( int index, int value )

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

public org.json.JSONArray put(int index,int value) throws org.json.JSONException

Parameters

index
value

Throws

put( int index, long value )

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

public org.json.JSONArray put(int index,long value) throws org.json.JSONException

Parameters

index
value

Throws

put( int index, Object value )

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

public org.json.JSONArray put(int index,Object value) throws org.json.JSONException

Parameters

index
value
a {@link JSONObject}, {@link JSONArray}, String, Boolean, Integer, Long, Double, {@link JSONObject#NULL}, or {@code null}. May not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.

Throws

isNull( int index )

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)

Parameters

index

get( int index )

Returns the value at {@code index}.

public java.lang.Object get(int index) throws org.json.JSONException

Parameters

index

Throws

opt( int index )

Returns the value at {@code index}, or null if the array has no value at {@code index}.

public java.lang.Object opt(int index)

Parameters

index

remove( int index )

Removes and returns the value at {@code index}, or null if the array has no value at {@code index}.

public java.lang.Object remove(int index)

Parameters

index

getBoolean( int index )

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

public boolean getBoolean(int index) throws org.json.JSONException

Parameters

index

Throws

optBoolean( int index )

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

public boolean optBoolean(int index)

Parameters

index

optBoolean( int index, boolean 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)

Parameters

index
fallback

getDouble( int index )

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

public double getDouble(int index) throws org.json.JSONException

Parameters

index

Throws

optDouble( int index )

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

public double optDouble(int index)

Parameters

index

optDouble( int index, double 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)

Parameters

index
fallback

getInt( int index )

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

public int getInt(int index) throws org.json.JSONException

Parameters

index

Throws

optInt( int index )

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

public int optInt(int index)

Parameters

index

optInt( int index, int 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)

Parameters

index
fallback

getLong( int index )

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

public long getLong(int index) throws org.json.JSONException

Parameters

index

Throws

optLong( int index )

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

public long optLong(int index)

Parameters

index

optLong( int index, long 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.

public long optLong(int index,long fallback)

Parameters

index
fallback

getString( int index )

Returns the value at {@code index} if it exists, coercing it if necessary.

public java.lang.String getString(int index) throws org.json.JSONException

Parameters

index

Throws

optString( int index )

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

public java.lang.String optString(int index)

Parameters

index

optString( int index, String fallback )

Returns the value at {@code index} if it exists, coercing it if necessary. Returns {@code fallback} if no such value exists.

public java.lang.String optString(int index,String fallback)

Parameters

index
fallback

getJSONArray( int index )

Returns the value at {@code index} if it exists and is a {@code JSONArray}.

public org.json.JSONArray getJSONArray(int index) throws org.json.JSONException

Parameters

index

Throws

optJSONArray( int index )

Returns the value at {@code index} if it exists and is a {@code JSONArray}. Returns null otherwise.

public org.json.JSONArray optJSONArray(int index)

Parameters

index

getJSONObject( int index )

Returns the value at {@code index} if it exists and is a {@code JSONObject}.

public org.json.JSONObject getJSONObject(int index) throws org.json.JSONException

Parameters

index

Throws

optJSONObject( int index )

Returns the value at {@code index} if it exists and is a {@code JSONObject}. Returns null otherwise.

public org.json.JSONObject optJSONObject(int index)

Parameters

index

toJSONObject( JSONArray names )

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

public org.json.JSONObject toJSONObject(JSONArray names) throws org.json.JSONException

Parameters

names

Throws

join( String separator )

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

public java.lang.String join(String separator) throws org.json.JSONException

Parameters

separator

Throws

toString()

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

[94043,90210]

public java.lang.String toString()

toString( int indentSpaces )

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

 [
     94043,
     90210
 ]

public java.lang.String toString(int indentSpaces) throws org.json.JSONException

Parameters

indentSpaces
the number of spaces to indent for each level of nesting.

Throws

equals( Object o )

public boolean equals(Object o)

Parameters

o

hashCode()

public int hashCode()

Inherited Methods

From Class Methods
java.lang.Object getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize
View on GitHub.com
Previous Section Next Section