Save This Page
Home » json » org.json » [javadoc | source]
org.json
public class: JSONArray [javadoc | source]
java.lang.Object
   org.json.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:

Constructor:
 public JSONArray() 
 public JSONArray(JSONTokener x) throws JSONException 
    Construct a JSONArray from a JSONTokener.
    Parameters:
    x - A JSONTokener
    Throws:
    JSONException - If there is a syntax error.
 public JSONArray(String source) throws JSONException 
    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.
 public JSONArray(Collection collection) 
    Construct a JSONArray from a Collection.
    Parameters:
    collection - A Collection.
 public JSONArray(Object array) throws JSONException 
    Construct a JSONArray from an array
    Throws:
    JSONException - If not an array.
 public JSONArray(Collection collection,
    boolean includeSuperClass) 
    Construct a JSONArray from a collection of beans. The collection should have Java Beans.
    Throws:
    JSONException - If not an array.
 public JSONArray(Object array,
    boolean includeSuperClass) throws JSONException 
    Construct a JSONArray from an array with a bean. The array should have Java Beans.
    Throws:
    JSONException - If not an array.
Method from org.json.JSONArray Summary:
get,   getBoolean,   getDouble,   getInt,   getJSONArray,   getJSONObject,   getLong,   getString,   isNull,   join,   length,   opt,   optBoolean,   optBoolean,   optDouble,   optDouble,   optInt,   optInt,   optJSONArray,   optJSONObject,   optLong,   optLong,   optString,   optString,   put,   put,   put,   put,   put,   put,   put,   put,   put,   put,   put,   put,   put,   put,   remove,   toJSONObject,   toString,   toString,   toString,   write
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.json.JSONArray Detail:
 public Object get(int index) throws JSONException 
    Get the object value associated with an index.
 public boolean getBoolean(int index) throws JSONException 
    Get the boolean value associated with an index. The string values "true" and "false" are converted to boolean.
 public double getDouble(int index) throws JSONException 
    Get the double value associated with an index.
 public int getInt(int index) throws JSONException 
    Get the int value associated with an index.
 public JSONArray getJSONArray(int index) throws JSONException 
    Get the JSONArray associated with an index.
 public JSONObject getJSONObject(int index) throws JSONException 
    Get the JSONObject associated with an index.
 public long getLong(int index) throws JSONException 
    Get the long value associated with an index.
 public String getString(int index) throws JSONException 
    Get the string associated with an index.
 public boolean isNull(int index) 
    Determine if the value is null.
 public String join(String separator) throws JSONException 
    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.
 public int length() 
    Get the number of elements in the JSONArray, included nulls.
 public Object opt(int index) 
    Get the optional object value associated with an index.
 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".
 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).
 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.
 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.
 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.
 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.
 public JSONArray optJSONArray(int index) 
    Get the optional JSONArray associated with an index.
 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.
 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.
 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.
 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.
 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.
 public JSONArray put(boolean value) 
    Append a boolean value. This increases the array's length by one.
 public JSONArray put(Collection value) 
    Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
 public JSONArray put(double value) throws JSONException 
    Append a double value. This increases the array's length by one.
 public JSONArray put(int value) 
    Append an int value. This increases the array's length by one.
 public JSONArray put(long value) 
    Append an long value. This increases the array's length by one.
 public JSONArray put(Map value) 
    Put a value in the JSONArray, where the value will be a JSONObject which is produced from a Map.
 public JSONArray put(Object value) 
    Append an object value. This increases the array's length by one.
 public JSONArray put(int index,
    boolean value) throws JSONException 
    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.
 public JSONArray put(int index,
    Collection value) throws JSONException 
    Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
 public JSONArray put(int index,
    double value) throws JSONException 
    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.
 public JSONArray put(int index,
    int value) throws JSONException 
    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.
 public JSONArray put(int index,
    long value) throws JSONException 
    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.
 public JSONArray put(int index,
    Map value) throws JSONException 
    Put a value in the JSONArray, where the value will be a JSONObject which is produced from a Map.
 public JSONArray put(int index,
    Object value) throws JSONException 
    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.
 public Object remove(int index) 
    Remove an index and close the hole.
 public JSONObject toJSONObject(JSONArray names) throws JSONException 
    Produce a JSONObject by combining a JSONArray of names with the values of this JSONArray.
 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.

 public String toString(int indentFactor) throws JSONException 
    Make a prettyprinted JSON text of this JSONArray. Warning: This method assumes that the data structure is acyclical.
 String toString(int indentFactor,
    int indent) throws JSONException 
    Make a prettyprinted JSON text of this JSONArray. Warning: This method assumes that the data structure is acyclical.
 public Writer write(Writer writer) throws JSONException 
    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.