processing.data
Class JSONArray
- java.lang.Object
-
- processing.data.JSONArray
-
public class JSONArray extends java.lang.ObjectA 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 havinggetandoptmethods for accessing the values by index, andputmethods for adding or replacing values. The values can be any of these types:Boolean,JSONArray,JSONObject,Number,String, or theJSONObject.NULL object.The constructor can convert a JSON text into a Java object. The
toStringmethod converts to JSON text.A
getmethod returns a value if one can be found, and throws an exception if one cannot be found. Anoptmethod returns a default value instead of throwing an exception, and so is useful for obtaining optional values.The generic
get()andopt()methods return an object which you can cast or query for type. There are also typedgetandoptmethods that do type checking and type coercion for you.The texts produced by the
toStringmethods 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
nullvalue 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).
- An extra
-
-
Constructor Summary
Constructors Constructor and Description JSONArray()Construct an empty JSONArray.JSONArray(FloatList list)JSONArray(IntList list)JSONArray(java.io.Reader reader)JSONArray(StringList list)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description JSONArrayappend(boolean value)Append a boolean value.JSONArrayappend(double value)Append a double value.JSONArrayappend(float value)Append a float value.JSONArrayappend(int value)Append an int value.JSONArrayappend(JSONArray value)JSONArrayappend(JSONObject value)JSONArrayappend(long value)Append an long value.JSONArrayappend(java.lang.String value)Append an String value.java.lang.Stringformat(int indentFactor)Make a pretty-printed JSON text of this JSONArray.booleangetBoolean(int index)Get the boolean value associated with an index.booleangetBoolean(int index, boolean defaultValue)Get the optional boolean value associated with an index.boolean[]getBooleanArray()Get this entire array as a boolean array.doublegetDouble(int index)Get the double value associated with an index.doublegetDouble(int index, double defaultValue)Get the optional double value associated with an index.double[]getDoubleArray()Get this entire array as a double array.floatgetFloat(int index)Get a value from an index as a float.floatgetFloat(int index, float defaultValue)float[]getFloatArray()Get this entire array as a float array.intgetInt(int index)Get the int value associated with an index.intgetInt(int index, int defaultValue)Get the optional int value associated with an index.int[]getIntArray()Get this entire array as an int array.JSONArraygetJSONArray(int index)Get the JSONArray associated with an index.JSONArraygetJSONArray(int index, JSONArray defaultValue)JSONObjectgetJSONObject(int index)Get the JSONObject associated with an index.JSONObjectgetJSONObject(int index, JSONObject defaultValue)longgetLong(int index)Get the long value associated with an index.longgetLong(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.StringgetString(int index)Get the string associated with an index.java.lang.StringgetString(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.booleanisNull(int index)Determine if the value is null.java.lang.Stringjoin(java.lang.String separator)Make a string from the contents of this JSONArray.static JSONArrayparse(java.lang.String source)Construct a JSONArray from a source JSON text.java.lang.Objectremove(int index)Remove an index and close the hole.booleansave(java.io.File file, java.lang.String options)JSONArraysetBoolean(int index, boolean value)Put or replace a boolean value in the JSONArray.JSONArraysetDouble(int index, double value)Put or replace a double value.JSONArraysetFloat(int index, float value)Put or replace a float value.JSONArraysetInt(int index, int value)Put or replace an int value.JSONArraysetJSONArray(int index, JSONArray value)JSONArraysetJSONObject(int index, JSONObject value)JSONArraysetLong(int index, long value)Put or replace a long value.JSONArraysetString(int index, java.lang.String value)Put or replace a String value.intsize()Get the number of elements in the JSONArray, included nulls.java.lang.StringtoString()Return the JSON data formatted with two spaces for indents.booleanwrite(java.io.PrintWriter output)booleanwrite(java.io.PrintWriter output, java.lang.String options)
-
-
-
Constructor Detail
-
JSONArray
public JSONArray()
Construct an empty JSONArray.
-
JSONArray
public JSONArray(java.io.Reader reader)
-
JSONArray
public JSONArray(IntList list)
-
JSONArray
public JSONArray(FloatList list)
-
JSONArray
public JSONArray(StringList list)
-
-
Method Detail
-
parse
public static JSONArray parse(java.lang.String source)
Construct a JSONArray from a source JSON text.- Parameters:
source- A string that begins with[(left bracket) and ends with](right bracket).- Returns:
nullif there is a syntax error.
-
getString
public java.lang.String getString(int index)
Get the string associated with an index.- Parameters:
index- must be between 0 and length() - 1- Returns:
- A string value.
- Throws:
java.lang.RuntimeException- If there is no string value for the index.- See Also:
getInt(int),getFloat(int),getBoolean(int)
-
getString
public java.lang.String getString(int index, java.lang.String defaultValue)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)
Get the int value associated with an index.- Parameters:
index- must be between 0 and length() - 1- Returns:
- The value.
- Throws:
java.lang.RuntimeException- If the key is not found or if the value is not a number.- See Also:
getFloat(int),getString(int),getBoolean(int)
-
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:
java.lang.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)
Get a value from an index as a float. JSON uses 'double' values internally, so this is simply getDouble() cast to a float.- Parameters:
index- must be between 0 and length() - 1- See Also:
getInt(int),getString(int),getBoolean(int)
-
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:
java.lang.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)
Get the boolean value associated with an index. The string values "true" and "false" are converted to boolean.- Parameters:
index- must be between 0 and length() - 1- Returns:
- The truth.
- Throws:
java.lang.RuntimeException- If there is no value for the index or if the value is not convertible to boolean.- See Also:
getInt(int),getFloat(int),getString(int)
-
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
public JSONArray getJSONArray(int index)
Get the JSONArray associated with an index.- Parameters:
index- must be between 0 and length() - 1- Returns:
- A JSONArray value.
- Throws:
java.lang.RuntimeException- If there is no value for the index. or if the value is not a JSONArray- See Also:
getJSONObject(int),setJSONObject(int, JSONObject),setJSONArray(int, JSONArray)
-
getJSONObject
public JSONObject getJSONObject(int index)
Get the JSONObject associated with an index.- Parameters:
index- the index value of the object to get- Returns:
- A JSONObject value.
- Throws:
java.lang.RuntimeException- If there is no value for the index or if the value is not a JSONObject- See Also:
getJSONArray(int),setJSONObject(int, JSONObject),setJSONArray(int, JSONArray)
-
getJSONObject
public JSONObject getJSONObject(int index, JSONObject defaultValue)
-
getStringArray
public java.lang.String[] getStringArray()
Get this entire array as a String array.- See Also:
getIntArray()
-
getIntArray
public int[] getIntArray()
Get this entire array as an int array. Everything must be an int.- See Also:
getStringArray()
-
getLongArray
public long[] getLongArray()
Get this entire array as a long array. Everything must be an long.
-
getFloatArray
public float[] getFloatArray()
Get this entire array as a float array. Everything must be an float.
-
getDoubleArray
public double[] getDoubleArray()
Get this entire array as a double array. Everything must be an double.
-
getBooleanArray
public boolean[] getBooleanArray()
Get this entire array as a boolean array. Everything must be a boolean.
-
append
public JSONArray append(java.lang.String value)
Append an String value. This increases the array's length by one.- Parameters:
value- a String value- Returns:
- this.
- See Also:
size(),remove(int)
-
append
public JSONArray append(int value)
Append an int value. This increases the array's length by one.- Parameters:
value- an int value- Returns:
- this.
-
append
public JSONArray append(long value)
Append an long value. This increases the array's length by one.- Parameters:
value- A long value.- Returns:
- this.
-
append
public JSONArray append(float value)
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:
java.lang.RuntimeException- if the value is not finite.
-
append
public JSONArray append(double value)
Append a double value. This increases the array's length by one.- Parameters:
value- A double value.- Returns:
- this.
- Throws:
java.lang.RuntimeException- if the value is not finite.
-
append
public JSONArray append(boolean value)
Append a boolean value. This increases the array's length by one.- Parameters:
value- a boolean value- Returns:
- this.
-
append
public JSONArray append(JSONObject value)
- Parameters:
value- a JSONObject value
-
setString
public JSONArray setString(int index, java.lang.String value)
Put or replace a String 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- an index valuevalue- the value to assign- Returns:
- this.
- Throws:
java.lang.RuntimeException- If the index is negative.- See Also:
setInt(int, int),setFloat(int, float),setBoolean(int, boolean)
-
setInt
public JSONArray setInt(int index, int value)
Put or replace an int 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- an index valuevalue- the value to assign- Returns:
- this.
- Throws:
java.lang.RuntimeException- If the index is negative.- See Also:
setFloat(int, float),setString(int, String),setBoolean(int, boolean)
-
setLong
public JSONArray setLong(int index, long value)
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:
java.lang.RuntimeException- If the index is negative.
-
setFloat
public JSONArray setFloat(int index, float value)
Put or replace a float value. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out. There are no 'double' values in JSON, so this is passed to setDouble(value).- Parameters:
index- an index valuevalue- the value to assign- Returns:
- this.
- Throws:
java.lang.RuntimeException- If the index is negative or if the value is not finite.- See Also:
setInt(int, int),setString(int, String),setBoolean(int, boolean)
-
setDouble
public JSONArray setDouble(int index, double value)
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:
java.lang.RuntimeException- If the index is negative or if the value is not finite.
-
setBoolean
public JSONArray setBoolean(int index, boolean value)
Put or replace a boolean value in the JSONArray. 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- an index valuevalue- the value to assign- Returns:
- this.
- Throws:
java.lang.RuntimeException- If the index is negative.- See Also:
setInt(int, int),setFloat(int, float),setString(int, String)
-
setJSONArray
public JSONArray setJSONArray(int index, JSONArray value)
- Parameters:
index- the index value to targetvalue- the value to assign- See Also:
setJSONObject(int, JSONObject),getJSONObject(int),getJSONArray(int)
-
setJSONObject
public JSONArray setJSONObject(int index, JSONObject value)
- Parameters:
index- the index value to targetvalue- the value to assign- See Also:
setJSONArray(int, JSONArray),getJSONObject(int),getJSONArray(int)
-
size
public int size()
Get the number of elements in the JSONArray, included nulls.- Returns:
- The length (or size).
- See Also:
append(String),remove(int)
-
isNull
public boolean isNull(int index)
Determine if the value is null.- 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
public java.lang.Object remove(int index)
Remove an index and close the hole.- 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
public boolean save(java.io.File file, java.lang.String options)
-
write
public boolean write(java.io.PrintWriter output)
-
write
public boolean write(java.io.PrintWriter output, java.lang.String options)
-
toString
public java.lang.String 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.- Overrides:
toStringin classjava.lang.Object
-
format
public java.lang.String format(int indentFactor)
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
public java.lang.String join(java.lang.String separator)
Make a string from the contents of this JSONArray. Theseparatorstring 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:
java.lang.RuntimeException- If the array contains an invalid number.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG