Package processing.data
Class JSONArray
java.lang.Object
processing.data.JSONArray
A JSONArray stores an array of JSON objects. JSONArrays can
be generated from scratch, dynamically, or using data from an existing file.
JSON can also be output and saved to disk, as in the example above.
Advanced
A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values. The internal form is an object havingget
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:
- An extra
,
(comma) may appear just before the closing bracket. - The
null
value will be inserted when there is,
(comma) elision. - Strings may be quoted with
'
(single quote). - Strings do not need to be quoted at all if they do not begin with a quote
or single quote, and if they do not contain leading or trailing spaces, and
if they do not contain any of these characters:
{ } [ ] / \ : , = ; #
and if they do not look like numbers and if they are not the reserved wordstrue
,false
, ornull
. - Values can be separated by
;
(semicolon) as well as by,
(comma).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionappend
(boolean value) Append a boolean value.append
(double value) Append a double value.append
(float value) Append a float value.append
(int value) Append an int value.append
(long value) Append an long value.Appends a new value to the JSONArray, increasing the array's length by one.append
(JSONObject value) format
(int indentFactor) Make a pretty-printed JSON text of this JSONArray.get
(int index) Get the object value associated with an index.boolean
getBoolean
(int index) Gets the boolean value associated with the specified index.boolean
getBoolean
(int index, boolean defaultValue) Get the optional boolean value associated with an index.boolean[]
Use toBooleanArray() instead.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[]
Deprecated.float
getFloat
(int index) Gets the float value associated with the specified index.float
getFloat
(int index, float defaultValue) float[]
Deprecated.int
getInt
(int index) Gets the int value associated with the specified index.int
getInt
(int index, int defaultValue) Get the optional int value associated with an index.int[]
Deprecated.getJSONArray
(int index) Retrieves the JSONArray with the associated index value.getJSONArray
(int index, JSONArray defaultValue) getJSONObject
(int index) Retrieves the JSONObject with the associated index value.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[]
Deprecated.getString
(int index) Gets the String value associated with the specified index.Get the optional string associated with an index.String[]
Deprecated.boolean
isNull
(int index) Determines if the value associated with the index is null, that is has no defined value (false) or if it has a value (true).Make a string from the contents of this JSONArray.static JSONArray
Construct a JSONArray from a source JSON text.remove
(int index) Removes the element from a JSONArray in the specified index position.boolean
setBoolean
(int index, boolean value) Inserts a new value into the JSONArray at the specified index position.setDouble
(int index, double value) Put or replace a double value.setFloat
(int index, float value) Inserts a new value into the JSONArray at the specified index position.setInt
(int index, int value) Inserts a new value into the JSONArray at the specified index position.setJSONArray
(int index, JSONArray value) Sets the value of the JSONArray with the associated index value.setJSONObject
(int index, JSONObject value) Sets the value of the JSONObject with the index value.setLong
(int index, long value) Put or replace a long value.Inserts a new value into the JSONArray at the specified index position.int
size()
Gets the total number of elements in a JSONArray (inclusive of null elements).boolean[]
Get this entire array as a boolean array.double[]
Get this entire array as a double array.float[]
Get this entire array as a float array.int[]
Returns the entire JSONArray as an array of ints.long[]
Get this entire array as a long array.toString()
Return the JSON data formatted with two spaces for indents.String[]
Returns the entire JSONArray as an array of Strings.boolean
write
(PrintWriter output) boolean
write
(PrintWriter output, String options)
-
Constructor Details
-
JSONArray
public JSONArray()Construct an empty JSONArray. -
JSONArray
-
JSONArray
-
JSONArray
-
JSONArray
-
-
Method Details
-
parse
Construct a JSONArray from a source JSON text.- Parameters:
source
- A string that begins with[
(left bracket) and ends with]
(right bracket).- Returns:
null
if there is a syntax error.
-
get
Get the object value associated with an index.- Parameters:
index
- must be between 0 and length() - 1- Returns:
- An object value.
- Throws:
RuntimeException
- If there is no value for the index.
-
getString
Gets the String value associated with the specified index.- Parameters:
index
- must be between 0 and length() - 1- Returns:
- A string value.
- Throws:
RuntimeException
- If there is no string value for the index.- See Also:
-
getString
Get the optional string associated with an index. The defaultValue is returned if the key is not found.- Parameters:
index
- The index must be between 0 and length() - 1.defaultValue
- The default value.- Returns:
- A String value.
-
getInt
public int getInt(int index) Gets the int value associated with the specified index.- Parameters:
index
- must be between 0 and length() - 1- Returns:
- The value.
- Throws:
RuntimeException
- If the key is not found or if the value is not a number.- See Also:
-
getInt
public int getInt(int index, int defaultValue) Get the optional int value associated with an index. The defaultValue is returned if there is no value for the index, or if the value is not a number and cannot be converted to a number.- Parameters:
index
- The index must be between 0 and length() - 1.defaultValue
- The default value.- Returns:
- The value.
-
getLong
public long getLong(int index) Get the long value associated with an index.- Parameters:
index
- The index must be between 0 and length() - 1- Returns:
- The value.
- Throws:
RuntimeException
- If the key is not found or if the value cannot be converted to a number.
-
getLong
public long getLong(int index, long defaultValue) Get the optional long value associated with an index. The defaultValue is returned if there is no value for the index, or if the value is not a number and cannot be converted to a number.- Parameters:
index
- The index must be between 0 and length() - 1.defaultValue
- The default value.- Returns:
- The value.
-
getFloat
public float getFloat(int index) Gets the float value associated with the specified index.- Parameters:
index
- must be between 0 and length() - 1- See Also:
-
getFloat
public float getFloat(int index, float defaultValue) -
getDouble
public double getDouble(int index) Get the double value associated with an index.- Parameters:
index
- must be between 0 and length() - 1- Returns:
- The value.
- Throws:
RuntimeException
- If the key is not found or if the value cannot be converted to a number.
-
getDouble
public double getDouble(int index, double defaultValue) Get the optional double value associated with an index. The defaultValue is returned if there is no value for the index, or if the value is not a number and cannot be converted to a number.- Parameters:
index
- subscriptdefaultValue
- The default value.- Returns:
- The value.
-
getBoolean
public boolean getBoolean(int index) Gets the boolean value associated with the specified index.- Parameters:
index
- must be between 0 and length() - 1- Returns:
- The truth.
- Throws:
RuntimeException
- If there is no value for the index or if the value is not convertible to boolean.- See Also:
-
getBoolean
public boolean getBoolean(int index, boolean defaultValue) Get the optional boolean value associated with an index. It returns the defaultValue if there is no value at that index or if it is not a Boolean or the String "true" or "false" (case insensitive).- Parameters:
index
- The index must be between 0 and length() - 1.defaultValue
- A boolean default.- Returns:
- The truth.
-
getJSONArray
Retrieves the JSONArray with the associated index value.- Parameters:
index
- must be between 0 and length() - 1- Returns:
- A JSONArray value.
- Throws:
RuntimeException
- If there is no value for the index. or if the value is not a JSONArray- See Also:
-
getJSONArray
-
getJSONObject
Retrieves the JSONObject with the associated index value.- Parameters:
index
- the index value of the object to get- Returns:
- A JSONObject value.
- Throws:
RuntimeException
- If there is no value for the index or if the value is not a JSONObject- See Also:
-
getJSONObject
-
booleanValues
-
intValues
-
longValues
-
floatValues
-
doubleValues
-
arrayValues
-
objectValues
-
getStringArray
Deprecated.Use toStringArray() instead. -
toStringArray
Returns the entire JSONArray as an array of Strings. (All values in the array must be of the String type.)- See Also:
-
toStringList
-
getIntArray
Deprecated.Use toIntArray() instead. -
toIntArray
public int[] toIntArray()Returns the entire JSONArray as an array of ints. (All values in the array must be of the int type.)- See Also:
-
toIntList
-
getLongArray
Deprecated.Use toLongArray() instead. -
toLongArray
public long[] toLongArray()Get this entire array as a long array. Everything must be an long. -
toLongList
-
getFloatArray
Deprecated.Use toFloatArray() instead. -
toFloatArray
public float[] toFloatArray()Get this entire array as a float array. Everything must be an float. -
toFloatList
-
getDoubleArray
Deprecated.Use toDoubleArray() instead. -
toDoubleArray
public double[] toDoubleArray()Get this entire array as a double array. Everything must be an double. -
toDoubleList
-
getBooleanArray
public boolean[] getBooleanArray()Use toBooleanArray() instead. -
toBooleanArray
public boolean[] toBooleanArray()Get this entire array as a boolean array. Everything must be a boolean. -
append
Appends a new value to the JSONArray, increasing the array's length by one. New values may be of the following types: int, float, String, boolean, JSONObject, or JSONArray.- Parameters:
value
- a String value- Returns:
- this.
- See Also:
-
append
Append an int value. This increases the array's length by one.- Parameters:
value
- an int value- Returns:
- this.
-
append
Append an long value. This increases the array's length by one.- Parameters:
value
- A long value.- Returns:
- this.
-
append
Append a float value. This increases the array's length by one. This will store the value as a double, since there are no floats in JSON.- Parameters:
value
- a float value- Returns:
- this.
- Throws:
RuntimeException
- if the value is not finite.
-
append
Append a double value. This increases the array's length by one.- Parameters:
value
- A double value.- Returns:
- this.
- Throws:
RuntimeException
- if the value is not finite.
-
append
Append a boolean value. This increases the array's length by one.- Parameters:
value
- a boolean value- Returns:
- this.
-
append
- Parameters:
value
- a JSONArray value
-
append
- Parameters:
value
- a JSONObject value
-
setString
Inserts a new value into the JSONArray at the specified index position. If a value already exists in the specified position, the new value overwrites the old value. If the given index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.- Parameters:
index
- an index valuevalue
- the value to assign- Returns:
- this.
- Throws:
RuntimeException
- If the index is negative.- See Also:
-
setInt
Inserts a new value into the JSONArray at the specified index position. If a value already exists in the specified position, the new value overwrites the old value. If the given index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.- Parameters:
index
- an index valuevalue
- the value to assign- Returns:
- this.
- Throws:
RuntimeException
- If the index is negative.- See Also:
-
setLong
Put or replace a long value. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.- Parameters:
index
- The subscript.value
- A long value.- Returns:
- this.
- Throws:
RuntimeException
- If the index is negative.
-
setFloat
Inserts a new value into the JSONArray at the specified index position. If a value already exists in the specified position, the new value overwrites the old value. If the given index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.- Parameters:
index
- an index valuevalue
- the value to assign- Returns:
- this.
- Throws:
RuntimeException
- If the index is negative or if the value is not finite.- See Also:
-
setDouble
Put or replace a double value. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.- Parameters:
index
- The subscript.value
- A double value.- Returns:
- this.
- Throws:
RuntimeException
- If the index is negative or if the value is not finite.
-
setBoolean
Inserts a new value into the JSONArray at the specified index position. If a value already exists in the specified position, the new value overwrites the old value. If the given index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.- Parameters:
index
- an index valuevalue
- the value to assign- Returns:
- this.
- Throws:
RuntimeException
- If the index is negative.- See Also:
-
setJSONArray
Sets the value of the JSONArray with the associated index value.- Parameters:
index
- the index value to targetvalue
- the value to assign- See Also:
-
setJSONObject
Sets the value of the JSONObject with the index value.- Parameters:
index
- the index value to targetvalue
- the value to assign- See Also:
-
size
public int size()Gets the total number of elements in a JSONArray (inclusive of null elements).- Returns:
- The length (or size).
- See Also:
-
append(String)
remove(int)
-
isNull
public boolean isNull(int index) Determines if the value associated with the index is null, that is has no defined value (false) or if it has a value (true).- Parameters:
index
- must be between 0 and length() - 1- Returns:
- true if the value at the index is null, or if there is no value.
-
remove
Removes the element from a JSONArray in the specified index position. Returns either the value associated with the given index, or null, if there is no value.- Parameters:
index
- the index value of the element to be removed- Returns:
- The value that was associated with the index, or null if there was no value.
- See Also:
-
size()
append(String)
-
save
-
write
-
write
-
toString
Return the JSON data formatted with two spaces for indents. Chosen to do this since it's the most common case (e.g. with println()). Same as format(2). Use the format() function for more options. -
format
Make a pretty-printed JSON text of this JSONArray. Warning: This method assumes that the data structure is acyclical.- Parameters:
indentFactor
- The number of spaces to add to each level of indentation. Use -1 to specify no indentation and no newlines.- Returns:
- a printable, displayable, transmittable
representation of the object, beginning
with
[
(left bracket) and ending with]
(right bracket).
-
join
Make a string from the contents of this JSONArray. Theseparator
string is inserted between each element. Warning: This method assumes that the data structure is acyclic.- Parameters:
separator
- A string that will be inserted between the elements.- Returns:
- a string.
- Throws:
RuntimeException
- If the array contains an invalid number.
-