-
public class JSONArrayA dense indexed sequence of values. Values may be any mix of JSONObjects, other JSONArrays, Strings, Booleans, Integers, Longs, Doubles,
{@code null}or NULL. Values may not be NaNs, 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 . 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.
-
-
Constructor Summary
Constructors Constructor Description JSONArray()Creates a {@code JSONArray}with no values.JSONArray(Collection copyFrom)Creates a new {@code JSONArray}by copying all values from the givencollection.JSONArray(JSONTokener readFrom)Creates a new {@code JSONArray}with values from the next array in thetokener.JSONArray(String json)Creates a new {@code JSONArray}with values from the JSON string.JSONArray(Object array)Creates a new {@code JSONArray}with values from the given primitive array.
-
Method Summary
Modifier and Type Method Description intlength()Returns the number of values in this array. JSONArrayput(boolean value)Appends {@code value}to the end of this array.JSONArrayput(double value)Appends {@code value}to the end of this array.JSONArrayput(int value)Appends {@code value}to the end of this array.JSONArrayput(long value)Appends {@code value}to the end of this array.JSONArrayput(Object value)Appends {@code value}to the end of this array.JSONArrayput(int index, boolean value)Sets the value at {@code index}to{@code value}, null padding this arrayto the required length if necessary.JSONArrayput(int index, double value)Sets the value at {@code index}to{@code value}, null padding this arrayto the required length if necessary.JSONArrayput(int index, int value)Sets the value at {@code index}to{@code value}, null padding this arrayto the required length if necessary.JSONArrayput(int index, long value)Sets the value at {@code index}to{@code value}, null padding this arrayto the required length if necessary.JSONArrayput(int index, Object value)Sets the value at {@code index}to{@code value}, null padding this arrayto the required length if necessary.booleanisNull(int index)Returns true if this array has no value at {@code index}, or if its valueis the{@code null}reference or NULL.Objectget(int index)Returns the value at {@code index}.Objectopt(int index)Returns the value at {@code index}, or null if the array has no valueat{@code index}.Objectremove(int index)Removes and returns the value at {@code index}, or null if the array has no valueat{@code index}.booleangetBoolean(int index)Returns the value at {@code index}if it exists and is a boolean or canbe coerced to a boolean.booleanoptBoolean(int index)Returns the value at {@code index}if it exists and is a boolean or canbe coerced to a boolean.booleanoptBoolean(int index, boolean fallback)Returns the value at {@code index}if it exists and is a boolean or canbe coerced to a boolean.doublegetDouble(int index)Returns the value at {@code index}if it exists and is a double or canbe coerced to a double.doubleoptDouble(int index)Returns the value at {@code index}if it exists and is a double or canbe coerced to a double.doubleoptDouble(int index, double fallback)Returns the value at {@code index}if it exists and is a double or canbe coerced to a double.intgetInt(int index)Returns the value at {@code index}if it exists and is an int orcan be coerced to an int.intoptInt(int index)Returns the value at {@code index}if it exists and is an int orcan be coerced to an int.intoptInt(int index, int fallback)Returns the value at {@code index}if it exists and is an int orcan be coerced to an int.longgetLong(int index)Returns the value at {@code index}if it exists and is a long orcan be coerced to a long.longoptLong(int index)Returns the value at {@code index}if it exists and is a long orcan be coerced to a long.longoptLong(int index, long fallback)Returns the value at {@code index}if it exists and is a long orcan be coerced to a long.StringgetString(int index)Returns the value at {@code index}if it exists, coercing it ifnecessary.StringoptString(int index)Returns the value at {@code index}if it exists, coercing it ifnecessary.StringoptString(int index, String fallback)Returns the value at {@code index}if it exists, coercing it ifnecessary.JSONArraygetJSONArray(int index)Returns the value at {@code index}if it exists and is a{@code * JSONArray}.JSONArrayoptJSONArray(int index)Returns the value at {@code index}if it exists and is a{@code * JSONArray}.JSONObjectgetJSONObject(int index)Returns the value at {@code index}if it exists and is a{@code * JSONObject}.JSONObjectoptJSONObject(int index)Returns the value at {@code index}if it exists and is a{@code * JSONObject}.JSONObjecttoJSONObject(JSONArray names)Returns a new object whose values are the values in this array, and whosenames are the values in {@code names}.Stringjoin(String separator)Returns a new string by alternating this array's values with {@code * separator}.StringtoString()Encodes this array as a compact JSON string, such as: [94043,90210]StringtoString(int indentSpaces)Encodes this array as a human readable JSON string for debugging, suchas: [ 94043, 90210 ]booleanequals(Object o)inthashCode()-
-
Constructor Detail
-
JSONArray
JSONArray()
Creates a{@code JSONArray}with no values.
-
JSONArray
JSONArray(Collection copyFrom)
Creates a new{@code JSONArray}by copying all values from the givencollection.- Parameters:
copyFrom- a collection whose values are of supported types.Unsupported values are not permitted and will yield an array in aninconsistent state.
-
JSONArray
JSONArray(JSONTokener readFrom)
Creates a new{@code JSONArray}with values from the next array in thetokener.- Parameters:
readFrom- a tokener whose nextValue() method will yield a{@code JSONArray}.
-
JSONArray
JSONArray(String json)
Creates a new{@code JSONArray}with values from the JSON string.- Parameters:
json- a JSON-encoded string containing an array.
-
JSONArray
JSONArray(Object array)
Creates a new{@code JSONArray}with values from the given primitive array.
-
-
Method Detail
-
length
int length()
Returns the number of values in this array.
-
put
JSONArray put(double value)
Appends
{@code value}to the end of this array.- Parameters:
value- a finite value.
-
put
JSONArray put(Object value)
Appends
{@code value}to the end of this array.- Parameters:
value- a JSONObject, JSONArray, String, Boolean,Integer, Long, Double, NULL, or{@code null}.
-
put
JSONArray put(int index, boolean value)
Sets the value at
{@code index}to{@code value}, null padding this arrayto the required length if necessary. If a value already exists at{@code * index}, it will be replaced.
-
put
JSONArray put(int index, double value)
Sets the value at
{@code index}to{@code value}, null padding this arrayto the required length if necessary. If a value already exists at{@code * index}, it will be replaced.- Parameters:
value- a finite value.
-
put
JSONArray put(int index, int value)
Sets the value at
{@code index}to{@code value}, null padding this arrayto the required length if necessary. If a value already exists at{@code * index}, it will be replaced.
-
put
JSONArray put(int index, long value)
Sets the value at
{@code index}to{@code value}, null padding this arrayto the required length if necessary. If a value already exists at{@code * index}, it will be replaced.
-
put
JSONArray put(int index, Object value)
Sets the value at
{@code index}to{@code value}, null padding this arrayto the required length if necessary. If a value already exists at{@code * index}, it will be replaced.- Parameters:
value- a JSONObject, JSONArray, String, Boolean,Integer, Long, Double, NULL, or{@code null}.
-
isNull
boolean isNull(int index)
Returns true if this array has no value at
{@code index}, or if its valueis the{@code null}reference or NULL.
-
opt
Object opt(int index)
Returns the value at
{@code index}, or null if the array has no valueat{@code index}.
-
remove
Object remove(int index)
Removes and returns the value at
{@code index}, or null if the array has no valueat{@code index}.
-
getBoolean
boolean getBoolean(int index)
Returns the value at
{@code index}if it exists and is a boolean or canbe coerced to a boolean.
-
optBoolean
boolean optBoolean(int index)
Returns the value at
{@code index}if it exists and is a boolean or canbe coerced to a boolean. Returns false otherwise.
-
optBoolean
boolean optBoolean(int index, boolean fallback)
Returns the value at
{@code index}if it exists and is a boolean or canbe coerced to a boolean. Returns{@code fallback}otherwise.
-
getDouble
double getDouble(int index)
Returns the value at
{@code index}if it exists and is a double or canbe coerced to a double.
-
optDouble
double optDouble(int index)
Returns the value at
{@code index}if it exists and is a double or canbe coerced to a double. Returns{@code NaN}otherwise.
-
optDouble
double optDouble(int index, double fallback)
Returns the value at
{@code index}if it exists and is a double or canbe coerced to a double. Returns{@code fallback}otherwise.
-
getInt
int getInt(int index)
Returns the value at
{@code index}if it exists and is an int orcan be coerced to an int.
-
optInt
int optInt(int index)
Returns the value at
{@code index}if it exists and is an int orcan be coerced to an int. Returns 0 otherwise.
-
optInt
int optInt(int index, int fallback)
Returns the value at
{@code index}if it exists and is an int orcan be coerced to an int. Returns{@code fallback}otherwise.
-
getLong
long getLong(int index)
Returns the value at
{@code index}if it exists and is a long orcan be coerced to a long.
-
optLong
long optLong(int index)
Returns the value at
{@code index}if it exists and is a long orcan be coerced to a long. Returns 0 otherwise.
-
optLong
long optLong(int index, long fallback)
Returns the value at
{@code index}if it exists and is a long orcan be coerced to a long. Returns{@code fallback}otherwise.
-
getString
String getString(int index)
Returns the value at
{@code index}if it exists, coercing it ifnecessary.
-
optString
String optString(int index)
Returns the value at
{@code index}if it exists, coercing it ifnecessary. Returns the empty string if no such value exists.
-
optString
String optString(int index, String fallback)
Returns the value at
{@code index}if it exists, coercing it ifnecessary. Returns{@code fallback}if no such value exists.
-
getJSONArray
JSONArray getJSONArray(int index)
Returns the value at
{@code index}if it exists and is a{@code * JSONArray}.
-
optJSONArray
JSONArray optJSONArray(int index)
Returns the value at
{@code index}if it exists and is a{@code * JSONArray}. Returns null otherwise.
-
getJSONObject
JSONObject getJSONObject(int index)
Returns the value at
{@code index}if it exists and is a{@code * JSONObject}.
-
optJSONObject
JSONObject optJSONObject(int index)
Returns the value at
{@code index}if it exists and is a{@code * JSONObject}. Returns null otherwise.
-
toJSONObject
JSONObject toJSONObject(JSONArray names)
Returns a new object whose values are the values in this array, and whosenames are the values in
{@code names}. Names and values are paired up byindex from 0 through to the shorter array's length. Names that are notstrings will be coerced to strings. This method returns null if eitherarray is empty.
-
join
String 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 specialcharacters escaped. For example, the array containing the strings '12"pizza', 'taco' and 'soda' joined on '+' returns this:"12\" pizza"+"taco"+"soda"
-
toString
String toString(int indentSpaces)
Encodes this array as a human readable JSON string for debugging, suchas:
[ 94043, 90210 ]- Parameters:
indentSpaces- the number of spaces to indent for each level ofnesting.
-
hashCode
int hashCode()
-
-
-
-