Package org.json
Class JSONArray
- java.lang.Object
-
- org.json.JSONArray
-
public class JSONArray extends java.lang.Object
A dense indexed sequence of values. Values may be any mix ofJSONObjects
, otherJSONArrays
, Strings, Booleans, Integers, Longs, Doubles,null
orJSONObject.NULL
. Values may not beNaNs
,infinities
, or of any type not listed here.JSONArray
has the same type coercion behavior and optional/mandatory accessors asJSONObject
. See that class' documentation for details.Warning: this class represents null in two incompatible ways: the standard Java
null
reference, and the sentinel valueJSONObject.NULL
. In particular,get
fails if the requested index holds the null reference, but succeeds if it holdsJSONObject.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.
-
-
Constructor Summary
Constructors Constructor Description JSONArray()
Creates aJSONArray
with no values.JSONArray(java.lang.Object array)
Creates a newJSONArray
with values from the given primitive array.JSONArray(java.lang.String json)
Creates a newJSONArray
with values from the JSON string.JSONArray(java.util.Collection copyFrom)
Creates a newJSONArray
by copying all values from the given collection.JSONArray(JSONTokener readFrom)
Creates a newJSONArray
with values from the next array in the tokener.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object o)
java.lang.Object
get(int index)
Returns the value atindex
.boolean
getBoolean(int index)
Returns the value atindex
if it exists and is a boolean or can be coerced to a boolean.double
getDouble(int index)
Returns the value atindex
if it exists and is a double or can be coerced to a double.int
getInt(int index)
Returns the value atindex
if it exists and is an int or can be coerced to an int.JSONArray
getJSONArray(int index)
Returns the value atindex
if it exists and is aJSONArray
.JSONObject
getJSONObject(int index)
Returns the value atindex
if it exists and is aJSONObject
.long
getLong(int index)
Returns the value atindex
if it exists and is a long or can be coerced to a long.java.lang.String
getString(int index)
Returns the value atindex
if it exists, coercing it if necessary.int
hashCode()
boolean
isNull(int index)
Returns true if this array has no value atindex
, or if its value is thenull
reference orJSONObject.NULL
.java.lang.String
join(java.lang.String separator)
Returns a new string by alternating this array's values withseparator
.int
length()
Returns the number of values in this array.java.lang.Object
opt(int index)
Returns the value atindex
, or null if the array has no value atindex
.boolean
optBoolean(int index)
Returns the value atindex
if it exists and is a boolean or can be coerced to a boolean.boolean
optBoolean(int index, boolean fallback)
Returns the value atindex
if it exists and is a boolean or can be coerced to a boolean.double
optDouble(int index)
Returns the value atindex
if it exists and is a double or can be coerced to a double.double
optDouble(int index, double fallback)
Returns the value atindex
if it exists and is a double or can be coerced to a double.int
optInt(int index)
Returns the value atindex
if it exists and is an int or can be coerced to an int.int
optInt(int index, int fallback)
Returns the value atindex
if it exists and is an int or can be coerced to an int.JSONArray
optJSONArray(int index)
Returns the value atindex
if it exists and is aJSONArray
.JSONObject
optJSONObject(int index)
Returns the value atindex
if it exists and is aJSONObject
.long
optLong(int index)
Returns the value atindex
if it exists and is a long or can be coerced to a long.long
optLong(int index, long fallback)
Returns the value atindex
if it exists and is a long or can be coerced to a long.java.lang.String
optString(int index)
Returns the value atindex
if it exists, coercing it if necessary.java.lang.String
optString(int index, java.lang.String fallback)
Returns the value atindex
if it exists, coercing it if necessary.JSONArray
put(boolean value)
Appendsvalue
to the end of this array.JSONArray
put(double value)
Appendsvalue
to the end of this array.JSONArray
put(int value)
Appendsvalue
to the end of this array.JSONArray
put(int index, boolean value)
Sets the value atindex
tovalue
, null padding this array to the required length if necessary.JSONArray
put(int index, double value)
Sets the value atindex
tovalue
, null padding this array to the required length if necessary.JSONArray
put(int index, int value)
Sets the value atindex
tovalue
, null padding this array to the required length if necessary.JSONArray
put(int index, long value)
Sets the value atindex
tovalue
, null padding this array to the required length if necessary.JSONArray
put(int index, java.lang.Object value)
Sets the value atindex
tovalue
, null padding this array to the required length if necessary.JSONArray
put(long value)
Appendsvalue
to the end of this array.JSONArray
put(java.lang.Object value)
Appendsvalue
to the end of this array.java.lang.Object
remove(int index)
Removes and returns the value atindex
, or null if the array has no value atindex
.JSONObject
toJSONObject(JSONArray names)
Returns a new object whose values are the values in this array, and whose names are the values innames
.java.lang.String
toString()
Encodes this array as a compact JSON string, such as:java.lang.String
toString(int indentSpaces)
Encodes this array as a human readable JSON string for debugging, such as:
-
-
-
Constructor Detail
-
JSONArray
public JSONArray()
Creates aJSONArray
with no values.
-
JSONArray
public JSONArray(java.util.Collection copyFrom)
Creates a newJSONArray
by copying all values from the given collection.- 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
public JSONArray(JSONTokener readFrom) throws JSONException
Creates a newJSONArray
with values from the next array in the tokener.- Parameters:
readFrom
- a tokener whose nextValue() method will yield aJSONArray
.- Throws:
JSONException
- if the parse fails or doesn't yield aJSONArray
.
-
JSONArray
public JSONArray(java.lang.String json) throws JSONException
Creates a newJSONArray
with values from the JSON string.- Parameters:
json
- a JSON-encoded string containing an array.- Throws:
JSONException
- if the parse fails or doesn't yield aJSONArray
.
-
JSONArray
public JSONArray(java.lang.Object array) throws JSONException
Creates a newJSONArray
with values from the given primitive array.- Throws:
JSONException
-
-
Method Detail
-
length
public int length()
Returns the number of values in this array.
-
put
public JSONArray put(boolean value)
Appendsvalue
to the end of this array.- Returns:
- this array.
-
put
public JSONArray put(double value) throws JSONException
Appendsvalue
to the end of this array.- Parameters:
value
- a finite value. May not beNaNs
orinfinities
.- Returns:
- this array.
- Throws:
JSONException
-
put
public JSONArray put(int value)
Appendsvalue
to the end of this array.- Returns:
- this array.
-
put
public JSONArray put(long value)
Appendsvalue
to the end of this array.- Returns:
- this array.
-
put
public JSONArray put(java.lang.Object value)
Appendsvalue
to the end of this array.- Parameters:
value
- aJSONObject
,JSONArray
, String, Boolean, Integer, Long, Double,JSONObject.NULL
, ornull
. May not beNaNs
orinfinities
. Unsupported values are not permitted and will cause the array to be in an inconsistent state.- Returns:
- this array.
-
put
public JSONArray put(int index, boolean value) throws JSONException
Sets the value atindex
tovalue
, null padding this array to the required length if necessary. If a value already exists atindex
, it will be replaced.- Returns:
- this array.
- Throws:
JSONException
-
put
public JSONArray put(int index, double value) throws JSONException
Sets the value atindex
tovalue
, null padding this array to the required length if necessary. If a value already exists atindex
, it will be replaced.- Parameters:
value
- a finite value. May not beNaNs
orinfinities
.- Returns:
- this array.
- Throws:
JSONException
-
put
public JSONArray put(int index, int value) throws JSONException
Sets the value atindex
tovalue
, null padding this array to the required length if necessary. If a value already exists atindex
, it will be replaced.- Returns:
- this array.
- Throws:
JSONException
-
put
public JSONArray put(int index, long value) throws JSONException
Sets the value atindex
tovalue
, null padding this array to the required length if necessary. If a value already exists atindex
, it will be replaced.- Returns:
- this array.
- Throws:
JSONException
-
put
public JSONArray put(int index, java.lang.Object value) throws JSONException
Sets the value atindex
tovalue
, null padding this array to the required length if necessary. If a value already exists atindex
, it will be replaced.- Parameters:
value
- aJSONObject
,JSONArray
, String, Boolean, Integer, Long, Double,JSONObject.NULL
, ornull
. May not beNaNs
orinfinities
.- Returns:
- this array.
- Throws:
JSONException
-
isNull
public boolean isNull(int index)
Returns true if this array has no value atindex
, or if its value is thenull
reference orJSONObject.NULL
.
-
get
public java.lang.Object get(int index) throws JSONException
Returns the value atindex
.- Throws:
JSONException
- if this array has no value atindex
, or if that value is thenull
reference. This method returns normally if the value isJSONObject#NULL
.
-
opt
public java.lang.Object opt(int index)
Returns the value atindex
, or null if the array has no value atindex
.
-
remove
public java.lang.Object remove(int index)
Removes and returns the value atindex
, or null if the array has no value atindex
.
-
getBoolean
public boolean getBoolean(int index) throws JSONException
Returns the value atindex
if it exists and is a boolean or can be coerced to a boolean.- Throws:
JSONException
- if the value atindex
doesn't exist or cannot be coerced to a boolean.
-
optBoolean
public boolean optBoolean(int index)
Returns the value atindex
if it exists and is a boolean or can be coerced to a boolean. Returns false otherwise.
-
optBoolean
public boolean optBoolean(int index, boolean fallback)
Returns the value atindex
if it exists and is a boolean or can be coerced to a boolean. Returnsfallback
otherwise.
-
getDouble
public double getDouble(int index) throws JSONException
Returns the value atindex
if it exists and is a double or can be coerced to a double.- Throws:
JSONException
- if the value atindex
doesn't exist or cannot be coerced to a double.
-
optDouble
public double optDouble(int index)
Returns the value atindex
if it exists and is a double or can be coerced to a double. ReturnsNaN
otherwise.
-
optDouble
public double optDouble(int index, double fallback)
Returns the value atindex
if it exists and is a double or can be coerced to a double. Returnsfallback
otherwise.
-
getInt
public int getInt(int index) throws JSONException
Returns the value atindex
if it exists and is an int or can be coerced to an int.- Throws:
JSONException
- if the value atindex
doesn't exist or cannot be coerced to a int.
-
optInt
public int optInt(int index)
Returns the value atindex
if it exists and is an int or can be coerced to an int. Returns 0 otherwise.
-
optInt
public int optInt(int index, int fallback)
Returns the value atindex
if it exists and is an int or can be coerced to an int. Returnsfallback
otherwise.
-
getLong
public long getLong(int index) throws JSONException
Returns the value atindex
if it exists and is a long or can be coerced to a long.- Throws:
JSONException
- if the value atindex
doesn't exist or cannot be coerced to a long.
-
optLong
public long optLong(int index)
Returns the value atindex
if it exists and is a long or can be coerced to a long. Returns 0 otherwise.
-
optLong
public long optLong(int index, long fallback)
Returns the value atindex
if it exists and is a long or can be coerced to a long. Returnsfallback
otherwise.
-
getString
public java.lang.String getString(int index) throws JSONException
Returns the value atindex
if it exists, coercing it if necessary.- Throws:
JSONException
- if no such value exists.
-
optString
public java.lang.String optString(int index)
Returns the value atindex
if it exists, coercing it if necessary. Returns the empty string if no such value exists.
-
optString
public java.lang.String optString(int index, java.lang.String fallback)
Returns the value atindex
if it exists, coercing it if necessary. Returnsfallback
if no such value exists.
-
getJSONArray
public JSONArray getJSONArray(int index) throws JSONException
Returns the value atindex
if it exists and is aJSONArray
.- Throws:
JSONException
- if the value doesn't exist or is not aJSONArray
.
-
optJSONArray
public JSONArray optJSONArray(int index)
Returns the value atindex
if it exists and is aJSONArray
. Returns null otherwise.
-
getJSONObject
public JSONObject getJSONObject(int index) throws JSONException
Returns the value atindex
if it exists and is aJSONObject
.- Throws:
JSONException
- if the value doesn't exist or is not aJSONObject
.
-
optJSONObject
public JSONObject optJSONObject(int index)
Returns the value atindex
if it exists and is aJSONObject
. Returns null otherwise.
-
toJSONObject
public JSONObject toJSONObject(JSONArray names) throws JSONException
Returns a new object whose values are the values in this array, and whose names are the values innames
. 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.- Throws:
JSONException
-
join
public java.lang.String join(java.lang.String separator) throws JSONException
Returns a new string by alternating this array's values withseparator
. 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"
- Throws:
JSONException
-
toString
public java.lang.String toString()
Encodes this array as a compact JSON string, such as:[94043,90210]
- Overrides:
toString
in classjava.lang.Object
-
toString
public java.lang.String toString(int indentSpaces) throws JSONException
Encodes this array as a human readable JSON string for debugging, such as:[ 94043, 90210 ]
- Parameters:
indentSpaces
- the number of spaces to indent for each level of nesting.- Throws:
JSONException
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-