JSONArray

public class JSONArray

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 having 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:

  • 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 words true, false, or null.
  • Values can be separated by ; (semicolon) as well as by , (comma).
  • Numbers may have the 0x- (hex) prefix.
Author:JSON.org

Constructors

JSONArray

public JSONArray()

Construct an empty JSONArray.

JSONArray

public JSONArray(JSONTokener x)

Construct a JSONArray from a JSONTokener.

Parameters:
  • x – A JSONTokener
Throws:
  • JSONException – If there is a syntax error.

JSONArray

public JSONArray(String source)

Construct a JSONArray from a source JSON text.

Parameters:
  • source – A string that begins with [ (left bracket) and ends with ] (right bracket).
Throws:
  • JSONException – If there is a syntax error.

JSONArray

public JSONArray(Collection<Object> collection)

Construct a JSONArray from a Collection.

Parameters:
  • collection – A Collection.

JSONArray

public JSONArray(Object array)

Construct a JSONArray from an array

Throws:
  • JSONException – If not an array.

Methods

get

public Object get(int index)

Get the object value associated with an index.

Parameters:
  • index – The index must be between 0 and length() - 1.
Throws:
Returns:

An object 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 – The index must be between 0 and length() - 1.
Throws:
  • JSONException – If there is no value for the index or if the value is not convertible to boolean.
Returns:

The truth.

getDouble

public double getDouble(int index)

Get the double value associated with an index.

Parameters:
  • index – The index must be between 0 and length() - 1.
Throws:
  • JSONException – If the key is not found or if the value cannot be converted to a number.
Returns:

The value.

getInt

public int getInt(int index)

Get the int value associated with an index.

Parameters:
  • index – The index must be between 0 and length() - 1.
Throws:
  • JSONException – If the key is not found or if the value is not a number.
Returns:

The value.

getJSONArray

public JSONArray getJSONArray(int index)

Get the JSONArray associated with an index.

Parameters:
  • index – The index must be between 0 and length() - 1.
Throws:
  • JSONException – If there is no value for the index. or if the value is not a JSONArray
Returns:

A JSONArray value.

getJSONObject

public JSONObject getJSONObject(int index)

Get the JSONObject associated with an index.

Parameters:
  • index – subscript
Throws:
  • JSONException – If there is no value for the index or if the value is not a JSONObject
Returns:

A JSONObject 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.
Throws:
  • JSONException – If the key is not found or if the value cannot be converted to a number.
Returns:

The value.

getString

public String getString(int index)

Get the string associated with an index.

Parameters:
  • index – The index must be between 0 and length() - 1.
Throws:
Returns:

A string value.

isNull

public boolean isNull(int index)

Determine if the value is null.

Parameters:
  • index – The index must be between 0 and length() - 1.
Returns:

true if the value at the index is null, or if there is no value.

join

public String join(String separator)

Make a string from the contents of this JSONArray. The separator string is inserted between each element. Warning: This method assumes that the data structure is acyclical.

Parameters:
  • separator – A string that will be inserted between the elements.
Throws:
Returns:

a string.

length

public int length()

Get the number of elements in the JSONArray, included nulls.

Returns:The length (or size).

opt

public Object opt(int index)

Get the optional object value associated with an index.

Parameters:
  • index – The index must be between 0 and length() - 1.
Returns:

An object value, or null if there is no object at that index.

optBoolean

public boolean optBoolean(int index)

Get the optional boolean value associated with an index. It returns false if there is no value at that index, or if the value is not Boolean.TRUE or the String “true”.

Parameters:
  • index – The index must be between 0 and length() - 1.
Returns:

The truth.

optBoolean

public boolean optBoolean(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.

optDouble

public double optDouble(int index)

Get the optional double value associated with an index. NaN 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.
Returns:

The value.

optDouble

public double optDouble(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 – subscript
  • defaultValue – The default value.
Returns:

The value.

optInt

public int optInt(int index)

Get the optional int value associated with an index. Zero 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.
Returns:

The value.

optInt

public int optInt(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.

optJSONArray

public JSONArray optJSONArray(int index)

Get the optional JSONArray associated with an index.

Parameters:
  • index – subscript
Returns:

A JSONArray value, or null if the index has no value, or if the value is not a JSONArray.

optJSONObject

public JSONObject optJSONObject(int index)

Get the optional JSONObject associated with an index. Null is returned if the key is not found, or null if the index has no value, or if the value is not a JSONObject.

Parameters:
  • index – The index must be between 0 and length() - 1.
Returns:

A JSONObject value.

optLong

public long optLong(int index)

Get the optional long value associated with an index. Zero 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.
Returns:

The value.

optLong

public long optLong(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.

optString

public String optString(int index)

Get the optional string value associated with an index. It returns an empty string if there is no value at that index. If the value is not a string and is not null, then it is coverted to a string.

Parameters:
  • index – The index must be between 0 and length() - 1.
Returns:

A String value.

optString

public String optString(int index, 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.

put

public JSONArray put(boolean value)

Append a boolean value. This increases the array’s length by one.

Parameters:
  • value – A boolean value.
Returns:

this.

put

public JSONArray put(Collection<Object> value)

Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.

Parameters:
  • value – A Collection value.
Returns:

this.

put

public JSONArray put(double value)

Append a double value. This increases the array’s length by one.

Parameters:
  • value – A double value.
Throws:
Returns:

this.

put

public JSONArray put(int value)

Append an int value. This increases the array’s length by one.

Parameters:
  • value – An int value.
Returns:

this.

put

public JSONArray put(long value)

Append an long value. This increases the array’s length by one.

Parameters:
  • value – A long value.
Returns:

this.

put

public JSONArray put(Map value)

Put a value in the JSONArray, where the value will be a JSONObject which is produced from a Map.

Parameters:
  • value – A Map value.
Returns:

this.

put

public JSONArray put(Object value)

Append an object value. This increases the array’s length by one.

Parameters:
  • value – An object value. The value should be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
Returns:

this.

put

public JSONArray put(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 – The subscript.
  • value – A boolean value.
Throws:
Returns:

this.

put

public JSONArray put(int index, Collection<Object> value)

Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.

Parameters:
  • index – The subscript.
  • value – A Collection value.
Throws:
  • JSONException – If the index is negative or if the value is not finite.
Returns:

this.

put

public JSONArray put(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.
Throws:
  • JSONException – If the index is negative or if the value is not finite.
Returns:

this.

put

public JSONArray put(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 – The subscript.
  • value – An int value.
Throws:
Returns:

this.

put

public JSONArray put(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.
Throws:
Returns:

this.

put

public JSONArray put(int index, Map value)

Put a value in the JSONArray, where the value will be a JSONObject that is produced from a Map.

Parameters:
  • index – The subscript.
  • value – The Map value.
Throws:
  • JSONException – If the index is negative or if the the value is an invalid number.
Returns:

this.

put

public JSONArray put(int index, Object value)

Put or replace an object 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 – The subscript.
  • value – The value to put into the array. The value should be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
Throws:
  • JSONException – If the index is negative or if the the value is an invalid number.
Returns:

this.

remove

public Object remove(int index)

Remove an index and close the hole.

Parameters:
  • index – The index of the element to be removed.
Returns:

The value that was associated with the index, or null if there was no value.

toJSONObject

public JSONObject toJSONObject(JSONArray names)

Produce a JSONObject by combining a JSONArray of names with the values of this JSONArray.

Parameters:
  • names – A JSONArray containing a list of key strings. These will be paired with the values.
Throws:
Returns:

A JSONObject, or null if there are no names or if this JSONArray has no values.

toString

public String toString()

Make a JSON text of this JSONArray. For compactness, no unnecessary whitespace is added. If it is not possible to produce a syntactically correct JSON text then null will be returned instead. This could occur if the array contains an invalid number.

Warning: This method assumes that the data structure is acyclical.

Returns:a printable, displayable, transmittable representation of the array.

toString

public String toString(int indentFactor)

Make a prettyprinted 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.
Throws:
Returns:

a printable, displayable, transmittable representation of the object, beginning with [ (left bracket) and ending with ] (right bracket).

toString

String toString(int indentFactor, int indent)

Make a prettyprinted 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.
  • indent – The indention of the top level.
Throws:
Returns:

a printable, displayable, transmittable representation of the array.

write

public Writer write(Writer writer)

Write the contents of the JSONArray as JSON text to a writer. For compactness, no whitespace is added.

Warning: This method assumes that the data structure is acyclical.

Throws:
Returns:

The writer.