public class JSONArray
extends 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(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(String value)
Append an String value.
|
String |
format(int indentFactor)
Make a pretty-printed JSON text of this JSONArray.
|
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.
|
String |
getString(int index)
Get the string associated with an index.
|
String |
getString(int index,
String defaultValue)
Get the optional string associated with an index.
|
String[] |
getStringArray()
Get this entire array as a String array.
|
boolean |
isNull(int index)
Determine if the value is null.
|
String |
join(String separator)
Make a string from the contents of this JSONArray.
|
static JSONArray |
parse(String source)
Construct a JSONArray from a source JSON text.
|
Object |
remove(int index)
Remove an index and close the hole.
|
boolean |
save(File file,
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,
String value)
Put or replace a String value.
|
int |
size()
Get the number of elements in the JSONArray, included nulls.
|
String |
toString()
Return the JSON data formatted with two spaces for indents.
|
boolean |
write(PrintWriter output) |
boolean |
write(PrintWriter output,
String options) |
public JSONArray()
public JSONArray(Reader reader)
public JSONArray(IntList list)
public JSONArray(FloatList list)
public JSONArray(StringList list)
public static JSONArray parse(String source)
source
- A string that begins with
[
(left bracket)
and ends with ]
(right bracket).null
if there is a syntax error.public Object get(int index)
index
- must be between 0 and length() - 1RuntimeException
- If there is no value for the index.public String getString(int index)
index
- must be between 0 and length() - 1RuntimeException
- If there is no string value for the index.getInt(int)
,
getFloat(int)
,
getBoolean(int)
public String getString(int index, 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() - 1RuntimeException
- 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() - 1RuntimeException
- 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() - 1RuntimeException
- 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() - 1RuntimeException
- 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() - 1RuntimeException
- 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 getRuntimeException
- 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 String[] getStringArray()
getIntArray()
public int[] getIntArray()
getStringArray()
public long[] getLongArray()
public float[] getFloatArray()
public double[] getDoubleArray()
public boolean[] getBooleanArray()
public JSONArray append(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 valueRuntimeException
- if the value is not finite.public JSONArray append(double value)
value
- A double value.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, String value)
index
- an index valuevalue
- the value to assignRuntimeException
- 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 assignRuntimeException
- 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.RuntimeException
- If the index is negative.public JSONArray setFloat(int index, float value)
index
- an index valuevalue
- the value to assignRuntimeException
- 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.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 assignRuntimeException
- 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 Object remove(int index)
index
- the index value of the element to be removedsize()
,
append(String)
public boolean save(File file, String options)
public boolean write(PrintWriter output)
public boolean write(PrintWriter output, String options)
public String toString()
toString
in class Object
public 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 String join(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.RuntimeException
- If the array contains an invalid number.