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.
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)
Indicates whether some other object is "equal to" this one.
|
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 a hash code value for the 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)
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)
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
Object
|
clone()
Creates and returns a copy of this object.
|
boolean
|
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
void
|
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
final
Class<?>
|
getClass()
Returns the runtime class of this Object .
|
int
|
hashCode()
Returns a hash code value for the object.
|
final
void
|
notify()
Wakes up a single thread that is waiting on this object's
monitor.
|
final
void
|
notifyAll()
Wakes up all threads that are waiting on this object's monitor.
|
String
|
toString()
Returns a string representation of the object.
|
final
void
|
wait(long millis, int nanos)
Causes the current thread to wait until another thread invokes the
notify() method or the
notifyAll() method for this object, or
some other thread interrupts the current thread, or a certain
amount of real time has elapsed.
|
final
void
|
wait(long millis)
Causes the current thread to wait until either another thread invokes the
notify() method or the
notifyAll() method for this object, or a
specified amount of time has elapsed.
|
final
void
|
wait()
Causes the current thread to wait until another thread invokes the
notify() method or the
notifyAll() method for this object.
|
|
Public constructors
JSONArray
JSONArray ()
Creates a JSONArray
with no values.
JSONArray
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
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
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
JSONArray (Object array)
Creates a new JSONArray
with values from the given primitive array.
Public methods
equals
boolean equals (Object o)
Indicates whether some other object is "equal to" this one.
The equals
method implements an equivalence relation
on non-null object references:
- It is reflexive: for any non-null reference value
x
, x.equals(x)
should return
true
.
- It is symmetric: for any non-null reference values
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
- It is transitive: for any non-null reference values
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
- It is consistent: for any non-null reference values
x
and y
, multiple invocations of
x.equals(y)
consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
- For any non-null reference value
x
,
x.equals(null)
should return false
.
The equals
method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode
method, which states
that equal objects must have equal hash codes.
Parameters |
o |
Object :
the reference object with which to compare. |
Returns |
boolean |
true if this object is the same as the obj
argument; false otherwise. |
get
Object get (int index)
Returns the value at index
.
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
boolean getBoolean (int index)
Returns the value at index
if it exists and is a boolean or can
be coerced to a boolean.
Throws |
JSONException |
if the value at index doesn't exist or
cannot be coerced to a boolean.
|
getDouble
double getDouble (int index)
Returns the value at index
if it exists and is a double or can
be coerced to a double.
Throws |
JSONException |
if the value at index doesn't exist or
cannot be coerced to a double.
|
getInt
int getInt (int index)
Returns the value at index
if it exists and is an int or
can be coerced to an int.
Throws |
JSONException |
if the value at index doesn't exist or
cannot be coerced to a int.
|
getJSONArray
JSONArray getJSONArray (int index)
Returns the value at index
if it exists and is a JSONArray
.
Throws |
JSONException |
if the value doesn't exist or is not a JSONArray .
|
getJSONObject
JSONObject getJSONObject (int index)
Returns the value at index
if it exists and is a JSONObject
.
Throws |
JSONException |
if the value doesn't exist or is not a JSONObject .
|
getLong
long getLong (int index)
Returns the value at index
if it exists and is a long or
can be coerced to a long.
Throws |
JSONException |
if the value at index doesn't exist or
cannot be coerced to a long.
|
getString
String getString (int index)
Returns the value at index
if it exists, coercing it if
necessary.
hashCode
int hashCode ()
Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
HashMap
.
The general contract of hashCode
is:
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCode
method
must consistently return the same integer, provided no information
used in equals
comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
- If two objects are equal according to the
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
- It is not required that if two objects are unequal
according to the
equals(java.lang.Object)
method, then calling the hashCode
method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by
class Object
does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
JavaTM programming language.)
Returns |
int |
a hash code value for this object. |
isNull
boolean isNull (int index)
Returns true if this array has no value at index
, or if its value
is the null
reference or NULL
.
join
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
|
length
int length ()
Returns the number of values in this array.
opt
Object opt (int index)
Returns the value at index
, or null if the array has no value
at index
.
optBoolean
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.
optBoolean
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
|
optDouble
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.
optDouble
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
|
optInt
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.
optInt
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
|
optJSONArray
JSONArray optJSONArray (int index)
Returns the value at index
if it exists and is a JSONArray
. Returns null otherwise.
optJSONObject
JSONObject optJSONObject (int index)
Returns the value at index
if it exists and is a JSONObject
. Returns null otherwise.
optLong
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.
optLong
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
|
optString
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.
optString
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
|
put
JSONArray put (int value)
Appends value
to the end of this array.
put
JSONArray put (long value)
Appends value
to the end of this array.
put
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
|
put
JSONArray put (double value)
Appends value
to the end of this array.
Parameters |
value |
double :
a finite value. May not be NaNs or
infinities . |
put
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.
put
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
|
put
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
|
put
JSONArray put (boolean value)
Appends value
to the end of this array.
put
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 . |
put
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. |
remove
Object remove (int index)
Removes and returns the value at index
, or null if the array has no value
at index
.
toJSONObject
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
|
toString
String toString ()
Encodes this array as a compact JSON string, such as:
[94043,90210]
Returns |
String |
a string representation of the object.
|
toString
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.
|