public class JSONArray
extends java.lang.Object
get
and opt
methods for accessing the values by index, and put
methods for
adding or replacing values. The values can be any of these types:
Boolean
, JSONArray
, JSONObject
,
Number
, String
, or the
JSONObject.NULL object
.
The constructor can convert a JSON text into a Java object. The
toString
method converts to JSON text.
A get
method returns a value if one can be found, and throws an
exception if one cannot be found. An opt
method returns a
default value instead of throwing an exception, and so is useful for
obtaining optional values.
The generic get()
and opt()
methods return an
object which you can cast or query for type. There are also typed
get
and opt
methods that do type checking and type
coercion for you.
The texts produced by the toString
methods strictly conform to
JSON syntax rules. The constructors are more forgiving in the texts they will
accept:
,
(comma) may appear just
before the closing bracket.null
value will be inserted when there is ,
(comma) elision.'
(single
quote).{ } [ ] / \ : , = ; #
and if they do not look like numbers and
if they are not the reserved words true
, false
, or
null
.;
(semicolon) as
well as by ,
(comma).Constructor and Description |
---|
JSONArray()
Construct an empty JSONArray.
|
JSONArray(FloatList list) |
JSONArray(IntList list) |
JSONArray(java.io.Reader reader) |
JSONArray(StringList list) |
Modifier and Type | Method and Description |
---|---|
JSONArray |
append(boolean value)
Append a boolean value.
|
JSONArray |
append(double value)
Append a double value.
|
JSONArray |
append(float value)
Append a float value.
|
JSONArray |
append(int value)
Append an int value.
|
JSONArray |
append(JSONArray value) |
JSONArray |
append(JSONObject value) |
JSONArray |
append(long value)
Append an long value.
|
JSONArray |
append(java.lang.String value)
Append an String value.
|
java.lang.String |
format(int indentFactor)
Make a pretty-printed JSON text of this JSONArray.
|
java.lang.Object |
get(int index)
Get the object value associated with an index.
|
boolean |
getBoolean(int index)
Get the boolean value associated with an index.
|
boolean |
getBoolean(int index,
boolean defaultValue)
Get the optional boolean value associated with an index.
|
boolean[] |
getBooleanArray()
Get this entire array as a boolean array.
|
double |
getDouble(int index)
Get the double value associated with an index.
|
double |
getDouble(int index,
double defaultValue)
Get the optional double value associated with an index.
|
double[] |
getDoubleArray()
Get this entire array as a double array.
|
float |
getFloat(int index)
Get a value from an index as a float.
|
float |
getFloat(int index,
float defaultValue) |
float[] |
getFloatArray()
Get this entire array as a float array.
|
int |
getInt(int index)
Get the int value associated with an index.
|
int |
getInt(int index,
int defaultValue)
Get the optional int value associated with an index.
|
int[] |
getIntArray()
Get this entire array as an int array.
|
JSONArray |
getJSONArray(int index)
Get the JSONArray associated with an index.
|
JSONArray |
getJSONArray(int index,
JSONArray defaultValue) |
JSONObject |
getJSONObject(int index)
Get the JSONObject associated with an index.
|
JSONObject |
getJSONObject(int index,
JSONObject defaultValue) |
long |
getLong(int index)
Get the long value associated with an index.
|
long |
getLong(int index,
long defaultValue)
Get the optional long value associated with an index.
|
long[] |
getLongArray()
Get this entire array as a long array.
|
java.lang.String |
getString(int index)
Get the string associated with an index.
|
java.lang.String |
getString(int index,
java.lang.String defaultValue)
Get the optional string associated with an index.
|
java.lang.String[] |
getStringArray()
Get this entire array as a String array.
|
boolean |
isNull(int index)
Determine if the value is null.
|
java.lang.String |
join(java.lang.String separator)
Make a string from the contents of this JSONArray.
|
static JSONArray |
parse(java.lang.String source)
Construct a JSONArray from a source JSON text.
|
java.lang.Object |
remove(int index)
Remove an index and close the hole.
|
boolean |
save(java.io.File file,
java.lang.String options) |
JSONArray |
setBoolean(int index,
boolean value)
Put or replace a boolean value in the JSONArray.
|
JSONArray |
setDouble(int index,
double value)
Put or replace a double value.
|
JSONArray |
setFloat(int index,
float value)
Put or replace a float value.
|
JSONArray |
setInt(int index,
int value)
Put or replace an int value.
|
JSONArray |
setJSONArray(int index,
JSONArray value) |
JSONArray |
setJSONObject(int index,
JSONObject value) |
JSONArray |
setLong(int index,
long value)
Put or replace a long value.
|
JSONArray |
setString(int index,
java.lang.String value)
Put or replace a String value.
|
int |
size()
Get the number of elements in the JSONArray, included nulls.
|
java.lang.String |
toString()
Return the JSON data formatted with two spaces for indents.
|
boolean |
write(java.io.PrintWriter output) |
boolean |
write(java.io.PrintWriter output,
java.lang.String options) |
public JSONArray()
public JSONArray(java.io.Reader reader)
public JSONArray(IntList list)
public JSONArray(FloatList list)
public JSONArray(StringList list)
public static JSONArray parse(java.lang.String source)
source
- A string that begins with
[
(left bracket)
and ends with ]
(right bracket).null
if there is a syntax error.public java.lang.Object get(int index)
index
- must be between 0 and length() - 1java.lang.RuntimeException
- If there is no value for the index.public java.lang.String getString(int index)
index
- must be between 0 and length() - 1java.lang.RuntimeException
- If there is no string value for the index.getInt(int)
,
getFloat(int)
,
getBoolean(int)
public java.lang.String getString(int index, java.lang.String defaultValue)
index
- The index must be between 0 and length() - 1.defaultValue
- The default value.public int getInt(int index)
index
- must be between 0 and length() - 1java.lang.RuntimeException
- If the key is not found or if the value is not a number.getFloat(int)
,
getString(int)
,
getBoolean(int)
public int getInt(int index, int defaultValue)
index
- The index must be between 0 and length() - 1.defaultValue
- The default value.public long getLong(int index)
index
- The index must be between 0 and length() - 1java.lang.RuntimeException
- If the key is not found or if the value cannot
be converted to a number.public long getLong(int index, long defaultValue)
index
- The index must be between 0 and length() - 1.defaultValue
- The default value.public float getFloat(int index)
index
- must be between 0 and length() - 1getInt(int)
,
getString(int)
,
getBoolean(int)
public float getFloat(int index, float defaultValue)
public double getDouble(int index)
index
- must be between 0 and length() - 1java.lang.RuntimeException
- If the key is not found or if the value cannot
be converted to a number.public double getDouble(int index, double defaultValue)
index
- subscriptdefaultValue
- The default value.public boolean getBoolean(int index)
index
- must be between 0 and length() - 1java.lang.RuntimeException
- If there is no value for the index or if the
value is not convertible to boolean.getInt(int)
,
getFloat(int)
,
getString(int)
public boolean getBoolean(int index, boolean defaultValue)
index
- The index must be between 0 and length() - 1.defaultValue
- A boolean default.public JSONArray getJSONArray(int index)
index
- must be between 0 and length() - 1java.lang.RuntimeException
- If there is no value for the index. or if the
value is not a JSONArraygetJSONObject(int)
,
setJSONObject(int, JSONObject)
,
setJSONArray(int, JSONArray)
public JSONObject getJSONObject(int index)
index
- the index value of the object to getjava.lang.RuntimeException
- If there is no value for the index or if the
value is not a JSONObjectgetJSONArray(int)
,
setJSONObject(int, JSONObject)
,
setJSONArray(int, JSONArray)
public JSONObject getJSONObject(int index, JSONObject defaultValue)
public java.lang.String[] getStringArray()
getIntArray()
public int[] getIntArray()
getStringArray()
public long[] getLongArray()
public float[] getFloatArray()
public double[] getDoubleArray()
public boolean[] getBooleanArray()
public JSONArray append(java.lang.String value)
value
- a String valuesize()
,
remove(int)
public JSONArray append(int value)
value
- an int valuepublic JSONArray append(long value)
value
- A long value.public JSONArray append(float value)
value
- a float valuejava.lang.RuntimeException
- if the value is not finite.public JSONArray append(double value)
value
- A double value.java.lang.RuntimeException
- if the value is not finite.public JSONArray append(boolean value)
value
- a boolean valuepublic JSONArray append(JSONObject value)
value
- a JSONObject valuepublic JSONArray setString(int index, java.lang.String value)
index
- an index valuevalue
- the value to assignjava.lang.RuntimeException
- If the index is negative.setInt(int, int)
,
setFloat(int, float)
,
setBoolean(int, boolean)
public JSONArray setInt(int index, int value)
index
- an index valuevalue
- the value to assignjava.lang.RuntimeException
- If the index is negative.setFloat(int, float)
,
setString(int, String)
,
setBoolean(int, boolean)
public JSONArray setLong(int index, long value)
index
- The subscript.value
- A long value.java.lang.RuntimeException
- If the index is negative.public JSONArray setFloat(int index, float value)
index
- an index valuevalue
- the value to assignjava.lang.RuntimeException
- If the index is negative or if the value is
not finite.setInt(int, int)
,
setString(int, String)
,
setBoolean(int, boolean)
public JSONArray setDouble(int index, double value)
index
- The subscript.value
- A double value.java.lang.RuntimeException
- If the index is negative or if the value is
not finite.public JSONArray setBoolean(int index, boolean value)
index
- an index valuevalue
- the value to assignjava.lang.RuntimeException
- If the index is negative.setInt(int, int)
,
setFloat(int, float)
,
setString(int, String)
public JSONArray setJSONArray(int index, JSONArray value)
index
- the index value to targetvalue
- the value to assignsetJSONObject(int, JSONObject)
,
getJSONObject(int)
,
getJSONArray(int)
public JSONArray setJSONObject(int index, JSONObject value)
index
- the index value to targetvalue
- the value to assignsetJSONArray(int, JSONArray)
,
getJSONObject(int)
,
getJSONArray(int)
public int size()
append(String)
,
remove(int)
public boolean isNull(int index)
index
- must be between 0 and length() - 1public java.lang.Object remove(int index)
index
- the index value of the element to be removedsize()
,
append(String)
public boolean save(java.io.File file, java.lang.String options)
public boolean write(java.io.PrintWriter output)
public boolean write(java.io.PrintWriter output, java.lang.String options)
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String format(int indentFactor)
indentFactor
- The number of spaces to add to each level of
indentation. Use -1 to specify no indentation and no newlines.[
(left bracket) and ending
with ]
(right bracket).public java.lang.String join(java.lang.String separator)
separator
string is inserted between each element.
Warning: This method assumes that the data structure is acyclic.separator
- A string that will be inserted between the elements.java.lang.RuntimeException
- If the array contains an invalid number.