public final class JSONArray extends JSONCollection implements Collection<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.Constructor and Description |
---|
JSONArray()
Creates a
JSONArray with no values. |
JSONArray(Object... values)
Creates a new
JSONArray with values from the given primitive array. |
JSONArray(String json)
Creates a new
JSONArray with values from the JSON string. |
Modifier and Type | Method and Description |
---|---|
boolean |
add(Object value)
Appends
value to the end of this array. |
boolean |
addAll(Collection<? extends Object> collection)
Adds all objects from the collection into this JSONArray, using
add(Object) . |
void |
clear()
Removes all of the values from this JSONArray.
|
boolean |
contains(Object value)
Returns
true if this JSONArray contains the specified value. |
boolean |
containsAll(Collection<?> c)
Returns
true if this JSONArray contains all of the values
in the specified collection. |
boolean |
equals(Object o) |
static JSONArray |
from(Iterable<?> iterable)
Create a new array, and adds all values from the iterable to the array (using
putAll(Iterable) . |
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() |
boolean |
isEmpty()
Returns
true if this array contains no values. |
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 . |
Iterator<Object> |
iterator()
Returns an iterator over the values in this array in proper sequence.
|
int |
length()
Deprecated.
Use
size() instead. |
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(Object value)
Deprecated.
The use of
add(Object) is encouraged. |
JSONArray |
putAll(Iterable<?> collection)
Puts all objects from the collection into this JSONArray, using
put(Object) . |
Object |
remove(int index)
Removes and returns the value at
index , or null if the array has no value
at index . |
boolean |
remove(Object value)
Removes the first occurrence of the specified value from this JSONArray,
if it is present.
|
boolean |
removeAll(Collection<?> collection)
Removes from this JSONArray all of its values that are contained in the
specified collection.
|
boolean |
retainAll(Collection<?> collection)
Retains only the values in this JSONArray that are contained in the
specified collection.
|
int |
size()
Returns the number of values in this array.
|
Object[] |
toArray()
Returns an array containing all of the values in this JSONArray in proper
sequence.
|
<T> T[] |
toArray(T[] array)
Returns an array containing all of the values in this JSONArray in
proper sequence; the runtime type of the returned array is that of
the specified array.
|
List<Object> |
toList()
Returns an unmodifiable list of the contents of the array.
|
prettyPrint, print, print, toCompactString, toString, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
parallelStream, removeIf, spliterator, stream
public JSONArray()
JSONArray
with no values.public JSONArray(String json)
JSONArray
with values from the JSON string.json
- a JSON-encoded string containing an array.JSONSyntaxException
- if the parse failsJSONTypeMismatchException
- if it doesn't yield a
JSONArray
.public JSONArray(Object... values)
JSONArray
with values from the given primitive array.values
- The values to use.IllegalArgumentException
- if any of the values are non-finite double values (i.e. NaN or infinite)public static JSONArray from(Iterable<?> iterable)
putAll(Iterable)
.
This is implemented as a static method so as not to break the semantics of the existing JSONArray(Object...)
constructor.
Adding a constructor of type Iterable would change the meaning of new JSONArray(new JSONArray())
.iterable
- collection ot value to include, or nullpublic int length()
size()
instead.public int size()
Integer.MAX_VALUE
elements, returns
Integer.MAX_VALUE
.size
in interface Collection<Object>
public boolean isEmpty()
true
if this array contains no values.isEmpty
in interface Collection<Object>
true
if this array contains no valuespublic JSONArray put(Object value)
add(Object)
is encouraged.value
to the end of this array.value
- a JSONObject
, JSONArray
, String, Boolean,
Integer, Long, Double, or JSONObject.NULL
}. May
not be NaNs
or infinities
. Unsupported values are not permitted and will cause the
array to be in an inconsistent state.public boolean add(Object value)
value
to the end of this array.add
in interface Collection<Object>
value
- a JSONObject
, JSONArray
, String, Boolean,
Integer, Long, Double, or JSONObject.NULL
}. May
not be NaNs
or infinities
. Unsupported values are not permitted and will cause the
array to be in an inconsistent state.true
(as specified by Collection.add(E)
)public JSONArray put(int index, Object value)
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
.IllegalArgumentException
- If the value cannot be represented as a finite double value.ArrayIndexOutOfBoundsException
- if the index is lower than 0public boolean isNull(int index)
index
, or if its value
is the null
reference or JSONObject.NULL
.index
- Which value to check.public Object get(int index)
index
.index
- Which value to get.JSONArrayIndexOutOfBoundsException
- if the given index is out of bounds.JSONValueNotFoundException
- 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 Object remove(int index)
index
, or null if the array has no value
at index
.index
- Which value to remove.public boolean remove(Object value)
remove
in interface Collection<Object>
value
- value to be removed from this JSONArray, if presenttrue
if the element was removedpublic boolean removeAll(Collection<?> collection)
removeAll
in interface Collection<Object>
collection
- collection containing value to be removed from this JSONArraytrue
if this JSONArray changed as a result of the callNullPointerException
- if the specified collection is null.Collection.contains(Object)
public void clear()
clear
in interface Collection<Object>
public boolean retainAll(Collection<?> collection)
retainAll
in interface Collection<Object>
collection
- collection containing elements to be retained in this listtrue
if this list changed as a result of the callpublic boolean getBoolean(int index)
index
if it exists and is a boolean or can
be coerced to a boolean.index
- Which value to get.JSONTypeMismatchException
- if the value at index
doesn't exist or
cannot be coerced to a boolean.public double getDouble(int index)
index
if it exists and is a double or can
be coerced to a double.index
- Which value to get.JSONTypeMismatchException
- if the value at index
doesn't exist or
cannot be coerced to a double.public int getInt(int index)
index
if it exists and is an int or
can be coerced to an int.index
- Which value to get.JSONTypeMismatchException
- if the value at index
doesn't exist or
cannot be coerced to a int.public long getLong(int index)
index
if it exists and is a long or
can be coerced to a long.index
- Which value to get.JSONTypeMismatchException
- if the value at index
doesn't exist or
cannot be coerced to a long.public String getString(int index)
index
if it exists, coercing it if
necessary.index
- Which value to get.JSONTypeMismatchException
- if no such value exists.public JSONArray getJSONArray(int index)
index
if it exists and is a JSONArray
.index
- Which value to get.JSONTypeMismatchException
- if the value doesn't exist or is not a JSONArray
.public JSONObject getJSONObject(int index)
index
if it exists and is a JSONObject
.index
- Which value to get.JSONTypeMismatchException
- if the value doesn't exist or is not a JSONObject
.public boolean equals(Object o)
equals
in interface Collection<Object>
equals
in class Object
public int hashCode()
hashCode
in interface Collection<Object>
hashCode
in class Object
public JSONArray putAll(Iterable<?> collection)
put(Object)
.collection
- List, array, JSONArray, or other iterable object, or nullpublic boolean addAll(Collection<? extends Object> collection)
add(Object)
.addAll
in interface Collection<Object>
collection
- Any collection, or nullpublic List<Object> toList()
public Object[] toArray()
toArray
in interface Collection<Object>
public <T> T[] toArray(T[] array)
toArray
in interface Collection<Object>
array
- the array into which the values of this JSONArray are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.ArrayStoreException
- if the runtime type of the specified array
is not a supertype of the runtime type of every element in
this listNullPointerException
- if the specified array is nullpublic Iterator<Object> iterator()
public boolean contains(Object value)
true
if this JSONArray contains the specified value.contains
in interface Collection<Object>
value
- value whose presence in this JSONArray is to be testedtrue
if this JSONArray contains the specified
valuepublic boolean containsAll(Collection<?> c)
true
if this JSONArray contains all of the values
in the specified collection.containsAll
in interface Collection<Object>
c
- collection to be checked for containment in this collectiontrue
if this collection contains all of the elements
in the specified collectionNullPointerException
- if the specified collection is null.contains(Object)
5.6.3 - Copyright © 2003-2021 The Apache Software Foundation.