Package 

Class JSONArray


  • 
    public class JSONArray
    
                        

    A dense indexed sequence of values. Values may be any mix of JSONObjects, other JSONArrays, Strings, Booleans, Integers, Longs, Doubles, {@code null} or NULL. Values may not be NaNs, infinities, or of any type not listed here.

    {@code JSONArray} has the same type coercion behavior and optional/mandatory accessors as JSONObject. See that class' documentation for details.

    Warning: this class represents null in two incompatible ways: the standard Java {@code null} reference, and the sentinel value . In particular, {@code get} fails if the requested index holds the null reference, but succeeds if it holds {@code JSONObject.NULL}.

    Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overridable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.

    • Constructor Summary

      Constructors 
      Constructor Description
      JSONArray() Creates a {@code JSONArray} with no values.
      JSONArray(Collection copyFrom) Creates a new {@code JSONArray} by copying all values from the givencollection.
      JSONArray(JSONTokener readFrom) Creates a new {@code JSONArray} with values from the next array in thetokener.
      JSONArray(String json) Creates a new {@code JSONArray} with values from the JSON string.
      JSONArray(Object array) Creates a new {@code JSONArray} with values from the given primitive array.
    • Method Summary

      Modifier and Type Method Description
      int length() Returns the number of values in this array.
      JSONArray put(boolean value) Appends {@code value} to the end of this array.
      JSONArray put(double value) Appends {@code value} to the end of this array.
      JSONArray put(int value) Appends {@code value} to the end of this array.
      JSONArray put(long value) Appends {@code value} to the end of this array.
      JSONArray put(Object value) Appends {@code value} to the end of this array.
      JSONArray put(int index, boolean value) Sets the value at {@code index} to {@code value}, null padding this arrayto the required length if necessary.
      JSONArray put(int index, double value) Sets the value at {@code index} to {@code value}, null padding this arrayto the required length if necessary.
      JSONArray put(int index, int value) Sets the value at {@code index} to {@code value}, null padding this arrayto the required length if necessary.
      JSONArray put(int index, long value) Sets the value at {@code index} to {@code value}, null padding this arrayto the required length if necessary.
      JSONArray put(int index, Object value) Sets the value at {@code index} to {@code value}, null padding this arrayto the required length if necessary.
      boolean isNull(int index) Returns true if this array has no value at {@code index}, or if its valueis the {@code null} reference or NULL.
      Object get(int index) Returns the value at {@code index}.
      Object opt(int index) Returns the value at {@code index}, or null if the array has no valueat {@code index}.
      Object remove(int index) Removes and returns the value at {@code index}, or null if the array has no valueat {@code index}.
      boolean getBoolean(int index) Returns the value at {@code index} if it exists and is a boolean or canbe coerced to a boolean.
      boolean optBoolean(int index) Returns the value at {@code index} if it exists and is a boolean or canbe coerced to a boolean.
      boolean optBoolean(int index, boolean fallback) Returns the value at {@code index} if it exists and is a boolean or canbe coerced to a boolean.
      double getDouble(int index) Returns the value at {@code index} if it exists and is a double or canbe coerced to a double.
      double optDouble(int index) Returns the value at {@code index} if it exists and is a double or canbe coerced to a double.
      double optDouble(int index, double fallback) Returns the value at {@code index} if it exists and is a double or canbe coerced to a double.
      int getInt(int index) Returns the value at {@code index} if it exists and is an int orcan be coerced to an int.
      int optInt(int index) Returns the value at {@code index} if it exists and is an int orcan be coerced to an int.
      int optInt(int index, int fallback) Returns the value at {@code index} if it exists and is an int orcan be coerced to an int.
      long getLong(int index) Returns the value at {@code index} if it exists and is a long orcan be coerced to a long.
      long optLong(int index) Returns the value at {@code index} if it exists and is a long orcan be coerced to a long.
      long optLong(int index, long fallback) Returns the value at {@code index} if it exists and is a long orcan be coerced to a long.
      String getString(int index) Returns the value at {@code index} if it exists, coercing it ifnecessary.
      String optString(int index) Returns the value at {@code index} if it exists, coercing it ifnecessary.
      String optString(int index, String fallback) Returns the value at {@code index} if it exists, coercing it ifnecessary.
      JSONArray getJSONArray(int index) Returns the value at {@code index} if it exists and is a {@code * JSONArray}.
      JSONArray optJSONArray(int index) Returns the value at {@code index} if it exists and is a {@code * JSONArray}.
      JSONObject getJSONObject(int index) Returns the value at {@code index} if it exists and is a {@code * JSONObject}.
      JSONObject optJSONObject(int index) Returns the value at {@code index} if it exists and is a {@code * JSONObject}.
      JSONObject toJSONObject(JSONArray names) Returns a new object whose values are the values in this array, and whosenames are the values in {@code names}.
      String join(String separator) Returns a new string by alternating this array's values with {@code * separator}.
      String toString() Encodes this array as a compact JSON string, such as:
      [94043,90210]
      String toString(int indentSpaces) Encodes this array as a human readable JSON string for debugging, suchas:
      [
          94043,
          90210
      ]
      boolean equals(Object o)
      int hashCode()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JSONArray

        JSONArray()
        Creates a {@code JSONArray} with no values.
      • JSONArray

        JSONArray(Collection copyFrom)
        Creates a new {@code JSONArray} by copying all values from the givencollection.
        Parameters:
        copyFrom - a collection whose values are of supported types.Unsupported values are not permitted and will yield an array in aninconsistent state.
      • JSONArray

        JSONArray(JSONTokener readFrom)
        Creates a new {@code JSONArray} with values from the next array in thetokener.
        Parameters:
        readFrom - a tokener whose nextValue() method will yield a{@code JSONArray}.
      • JSONArray

        JSONArray(String json)
        Creates a new {@code JSONArray} with values from the JSON string.
        Parameters:
        json - a JSON-encoded string containing an array.
      • JSONArray

        JSONArray(Object array)
        Creates a new {@code JSONArray} with values from the given primitive array.
    • Method Detail

      • length

         int length()

        Returns the number of values in this array.

      • put

         JSONArray put(boolean value)

        Appends {@code value} to the end of this array.

      • put

         JSONArray put(double value)

        Appends {@code value} to the end of this array.

        Parameters:
        value - a finite value.
      • put

         JSONArray put(int value)

        Appends {@code value} to the end of this array.

      • put

         JSONArray put(long value)

        Appends {@code value} to the end of this array.

      • put

         JSONArray put(int index, boolean value)

        Sets the value at {@code index} to {@code value}, null padding this arrayto the required length if necessary. If a value already exists at {@code * index}, it will be replaced.

      • put

         JSONArray put(int index, double value)

        Sets the value at {@code index} to {@code value}, null padding this arrayto the required length if necessary. If a value already exists at {@code * index}, it will be replaced.

        Parameters:
        value - a finite value.
      • put

         JSONArray put(int index, int value)

        Sets the value at {@code index} to {@code value}, null padding this arrayto the required length if necessary. If a value already exists at {@code * index}, it will be replaced.

      • put

         JSONArray put(int index, long value)

        Sets the value at {@code index} to {@code value}, null padding this arrayto the required length if necessary. If a value already exists at {@code * index}, it will be replaced.

      • put

         JSONArray put(int index, Object value)

        Sets the value at {@code index} to {@code value}, null padding this arrayto the required length if necessary. If a value already exists at {@code * index}, it will be replaced.

        Parameters:
        value - a JSONObject, JSONArray, String, Boolean,Integer, Long, Double, NULL, or {@code null}.
      • isNull

         boolean isNull(int index)

        Returns true if this array has no value at {@code index}, or if its valueis the {@code null} reference or NULL.

      • get

         Object get(int index)

        Returns the value at {@code index}.

      • opt

         Object opt(int index)

        Returns the value at {@code index}, or null if the array has no valueat {@code index}.

      • remove

         Object remove(int index)

        Removes and returns the value at {@code index}, or null if the array has no valueat {@code index}.

      • getBoolean

         boolean getBoolean(int index)

        Returns the value at {@code index} if it exists and is a boolean or canbe coerced to a boolean.

      • optBoolean

         boolean optBoolean(int index)

        Returns the value at {@code index} if it exists and is a boolean or canbe coerced to a boolean. Returns false otherwise.

      • optBoolean

         boolean optBoolean(int index, boolean fallback)

        Returns the value at {@code index} if it exists and is a boolean or canbe coerced to a boolean. Returns {@code fallback} otherwise.

      • getDouble

         double getDouble(int index)

        Returns the value at {@code index} if it exists and is a double or canbe coerced to a double.

      • optDouble

         double optDouble(int index)

        Returns the value at {@code index} if it exists and is a double or canbe coerced to a double. Returns {@code NaN} otherwise.

      • optDouble

         double optDouble(int index, double fallback)

        Returns the value at {@code index} if it exists and is a double or canbe coerced to a double. Returns {@code fallback} otherwise.

      • getInt

         int getInt(int index)

        Returns the value at {@code index} if it exists and is an int orcan be coerced to an int.

      • optInt

         int optInt(int index)

        Returns the value at {@code index} if it exists and is an int orcan be coerced to an int. Returns 0 otherwise.

      • optInt

         int optInt(int index, int fallback)

        Returns the value at {@code index} if it exists and is an int orcan be coerced to an int. Returns {@code fallback} otherwise.

      • getLong

         long getLong(int index)

        Returns the value at {@code index} if it exists and is a long orcan be coerced to a long.

      • optLong

         long optLong(int index)

        Returns the value at {@code index} if it exists and is a long orcan be coerced to a long. Returns 0 otherwise.

      • optLong

         long optLong(int index, long fallback)

        Returns the value at {@code index} if it exists and is a long orcan be coerced to a long. Returns {@code fallback} otherwise.

      • getString

         String getString(int index)

        Returns the value at {@code index} if it exists, coercing it ifnecessary.

      • optString

         String optString(int index)

        Returns the value at {@code index} if it exists, coercing it ifnecessary. Returns the empty string if no such value exists.

      • optString

         String optString(int index, String fallback)

        Returns the value at {@code index} if it exists, coercing it ifnecessary. Returns {@code fallback} if no such value exists.

      • getJSONArray

         JSONArray getJSONArray(int index)

        Returns the value at {@code index} if it exists and is a {@code * JSONArray}.

      • optJSONArray

         JSONArray optJSONArray(int index)

        Returns the value at {@code index} if it exists and is a {@code * JSONArray}. Returns null otherwise.

      • getJSONObject

         JSONObject getJSONObject(int index)

        Returns the value at {@code index} if it exists and is a {@code * JSONObject}.

      • optJSONObject

         JSONObject optJSONObject(int index)

        Returns the value at {@code index} if it exists and is a {@code * JSONObject}. Returns null otherwise.

      • toJSONObject

         JSONObject toJSONObject(JSONArray names)

        Returns a new object whose values are the values in this array, and whosenames are the values in {@code names}. Names and values are paired up byindex from 0 through to the shorter array's length. Names that are notstrings will be coerced to strings. This method returns null if eitherarray is empty.

      • join

         String join(String separator)

        Returns a new string by alternating this array's values with {@code * separator}. This array's string values are quoted and have their specialcharacters escaped. For example, the array containing the strings '12"pizza', 'taco' and 'soda' joined on '+' returns this:

        "12\" pizza"+"taco"+"soda"
      • toString

         String toString()

        Encodes this array as a compact JSON string, such as:

        [94043,90210]
      • toString

         String toString(int indentSpaces)

        Encodes this array as a human readable JSON string for debugging, suchas:

        [
            94043,
            90210
        ]
        Parameters:
        indentSpaces - the number of spaces to indent for each level ofnesting.