Class JSONArray

All Implemented Interfaces:
Serializable, Cloneable, Iterable<Object>, Collection<Object>, List<Object>, RandomAccess

public class JSONArray extends ArrayList<Object>
Represents a JSON array.
Since:
1.0.0
Version:
2.1.0
Author:
FangYidong(fangyidong@yahoo.com.cn), Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
See Also:
  • Constructor Details

    • JSONArray

      public JSONArray()
      Constructs an empty JSONArray.
      Since:
      1.0.0
    • JSONArray

      public JSONArray(Collection<?> collection)
      Constructs a JSONArray with the elements of the given Collection.
      Parameters:
      collection - the Collection
      Since:
      1.0.0
    • JSONArray

      public JSONArray(T[] array)
      Constructs a JSONArray with the elements of the given array.
      Type Parameters:
      T - the array type
      Parameters:
      array - the array
      Since:
      1.0.0
    • JSONArray

      public JSONArray(byte[] array)
      Constructs a JSONArray with the elements of the given array.
      Parameters:
      array - the array
      Since:
      1.0.0
    • JSONArray

      public JSONArray(boolean[] array)
      Constructs a JSONArray with the elements of the given array.
      Parameters:
      array - the array
      Since:
      1.0.0
    • JSONArray

      public JSONArray(char[] array)
      Constructs a JSONArray with the elements of the given array.
      Parameters:
      array - the array
      Since:
      1.0.0
    • JSONArray

      public JSONArray(short[] array)
      Constructs a JSONArray with the elements of the given array.
      Parameters:
      array - the array
      Since:
      1.0.0
    • JSONArray

      public JSONArray(int[] array)
      Constructs a JSONArray with the elements of the given array.
      Parameters:
      array - the array
      Since:
      1.0.0
    • JSONArray

      public JSONArray(long[] array)
      Constructs a JSONArray with the elements of the given array.
      Parameters:
      array - the array
      Since:
      1.0.0
    • JSONArray

      public JSONArray(float[] array)
      Constructs a JSONArray with the elements of the given array.
      Parameters:
      array - the array
      Since:
      1.0.0
    • JSONArray

      public JSONArray(double[] array)
      Constructs a JSONArray with the elements of the given array.
      Parameters:
      array - the array
      Since:
      1.0.0
    • JSONArray

      public JSONArray(Object array)
      Constructs a JSONArray with the elements of the given array. This constructor is a little slower than the others because it uses the Reflection API.
      Parameters:
      array - the array
      Since:
      1.0.0
    • JSONArray

      public JSONArray(String json) throws JSONParseException
      Constructs a JSONArray from a JSON string.
      Parameters:
      json - the JSON string
      Throws:
      JSONParseException - if the JSON is invalid
      Since:
      1.0.0
    • JSONArray

      public JSONArray(String json, boolean strict) throws JSONParseException
      Constructs a JSONArray from a JSON string.
      Parameters:
      json - the JSON string
      strict - true for strict validation of JSON data; false if missing colons and commas should be tolerated
      Throws:
      JSONParseException - if the JSON is invalid
      Since:
      2.1.0
    • JSONArray

      public JSONArray(Reader reader) throws JSONParseException, IOException
      Constructs a JSONArray from JSON data read from a Reader.
      Parameters:
      reader - the Reader
      Throws:
      JSONParseException - if the JSON is invalid
      IOException - if an I/O error occurred
      Since:
      1.0.0
    • JSONArray

      public JSONArray(Reader reader, boolean strict) throws JSONParseException, IOException
      Constructs a JSONArray from JSON data read from a Reader.
      Parameters:
      reader - the Reader
      strict - true for strict validation of JSON data; false if missing colons and commas should be tolerated
      Throws:
      JSONParseException - if the JSON is invalid
      IOException - if an I/O error occurred
      Since:
      2.1.0
  • Method Details

    • write

      public void write(Writer writer) throws IOException
      Writes this JSONArray as a JSON string on the given Writer.
      Parameters:
      writer - the Writer
      Throws:
      IOException - if an I/O error occurred
      Since:
      1.0.0
    • toString

      public String toString()
      Converts this JSONArray to a JSON string.
      Overrides:
      toString in class AbstractCollection<Object>
      Returns:
      this JSONArray as a JSON string
      Since:
      1.0.0
    • equals

      public boolean equals(Object object)
      Specified by:
      equals in interface Collection<Object>
      Specified by:
      equals in interface List<Object>
      Overrides:
      equals in class ArrayList<Object>
    • getObject

      public JSONObject getObject(int index)
      If the value is a JSONObject already, it will be casted and returned. If the value is a Map, it will be wrapped in a JSONObject. The wrapped JSONObject will be returned. In any other case this method returns null.
      Parameters:
      index - index of the value
      Returns:
      a JSONObject or null
      Since:
      1.0.0
    • getArray

      public JSONArray getArray(int index)
      If the value already is a JSONArray, it will be casted and returned. If the value is an array or Collection, it will be wrapped in a JSONArray. The JSONArray will be returned. In any other case this method returns null.
      Parameters:
      index - index of the value
      Returns:
      a JSONArray or null
      Since:
      1.0.0
    • getBoolean

      public Boolean getBoolean(int index)
      If the value is already a Boolean, it will be casted and returned. If the value is a String, it will be parsed. The result is returned. If the value is a Number, this method will return true in case its long value is 1. It will return false if it is a number but the long value is not 1. In any other case this method returns null.
      Parameters:
      index - index of the value
      Returns:
      a Boolean or null
      Since:
      1.0.0
    • getByte

      public Byte getByte(int index)
      If the value is a Number, its byte value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      index - index of the value
      Returns:
      a Byte or null
      Since:
      1.0.0
    • getShort

      public Short getShort(int index)
      If the value is a Number, its short value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      index - index of the value
      Returns:
      a Short or null
      Since:
      1.0.0
    • getInteger

      public Integer getInteger(int index)
      If the value is a Number, its int value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      index - index of the value
      Returns:
      a Integer or null
      Since:
      1.0.0
    • getLong

      public Long getLong(int index)
      If the value is a Number, its long value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      index - index of the value
      Returns:
      a Long or null
      Since:
      1.0.0
    • getFloat

      public Float getFloat(int index)
      If the value is a Number, its float value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      index - index of the value
      Returns:
      a Float or null
      Since:
      1.0.0
    • getDouble

      public Double getDouble(int index)
      If the value is a Number, its double value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns 1 in case the value is true otherwise 0. In any other case this method returns null.
      Parameters:
      index - index of the value
      Returns:
      a Double or null
      Since:
      1.0.0
    • getString

      public String getString(int index)
      Returns the same as the value's Object.toString() method. If the actual value is null, this method will return null.
      Parameters:
      index - index of the value
      Returns:
      a String or null
      Since:
      1.0.0
    • getDate

      public Date getDate(int index, DateFormat format) throws ParseException
      If the value already is a Date, it will be casted and returned. Otherwise the result of the value's Object.toString() will be parsed by the given DateFormat. The result is returned. If the actual value is null, this method will return null.
      Parameters:
      index - index of the value
      format - the DateFormat used to parse the date
      Returns:
      a Date or null
      Throws:
      ParseException - if the date could not be parsed
      Since:
      1.0.0
    • getEnum

      public <T extends Enum<T>> T getEnum(int index, Class<T> type)
      If the String representation of the value equals the name of the enum constant in the given enum type, it will return the enum constant. In any other case this method returns null.
      Type Parameters:
      T - the return type
      Parameters:
      index - index of the value
      type - the enum type
      Returns:
      an Enum or null
      Since:
      1.0.0
    • toBooleanArray

      public Boolean[] toBooleanArray()
      Converts this JSONArray into an array of Booleans using the getBoolean(int) method.
      Returns:
      an array Booleans
      Since:
      1.0.0
    • toByteArray

      public Byte[] toByteArray()
      Converts this JSONArray into an array of Bytes using the getByte(int) method.
      Returns:
      an array of Bytes
      Since:
      1.0.0
    • toShortArray

      public Short[] toShortArray()
      Converts this JSONArray into an array of Shorts using the getShort(int) method.
      Returns:
      an array of Shorts
      Since:
      1.0.0
    • toIntegerArray

      public Integer[] toIntegerArray()
      Converts this JSONArray into an array of Integers using the getInteger(int) method.
      Returns:
      an array of Integers
      Since:
      1.0.0
    • toLongArray

      public Long[] toLongArray()
      Converts this JSONArray into an array of Longs using the getLong(int) method.
      Returns:
      an array of Longs
      Since:
      1.0.0
    • toFloatArray

      public Float[] toFloatArray()
      Converts this JSONArray into an array of Floats using the getFloat(int) method.
      Returns:
      an array of Floats
      Since:
      1.0.0
    • toDoubleArray

      public Double[] toDoubleArray()
      Converts this JSONArray into an array of Doubles using the getDouble(int) method.
      Returns:
      an array of Doubles
      Since:
      1.0.0
    • toStringArray

      public String[] toStringArray()
      Converts this JSONArray into an array of Strings using the getString(int) method.
      Returns:
      an array of Strings
      Since:
      1.0.0
    • toObjectArray

      public JSONObject[] toObjectArray()
      Converts this JSONArray into an array of JSONObjects using the getObject(int) method.
      Returns:
      an array of JSONObjects
      Since:
      1.0.0
    • toArrayArray

      public JSONArray[] toArrayArray()
      Converts this JSONArray into an array of JSONArrays using the getArray(int) method.
      Returns:
      an array of JSONArrays.
      Since:
      1.0.0
    • toPrimitiveByteArray

      public byte[] toPrimitiveByteArray()
      Converts this JSONArray to a primitive byte array.
      Returns:
      a primitive byte array
      Since:
      2.0.0
    • toPrimitiveShortArray

      public short[] toPrimitiveShortArray()
      Converts this JSONArray to a primitive short array.
      Returns:
      a primitive short array
      Since:
      2.0.0
    • toPrimitiveIntArray

      public int[] toPrimitiveIntArray()
      Converts this JSONArray to a primitive int array.
      Returns:
      a primitive int array
      Since:
      2.0.0
    • toPrimitiveLongArray

      public long[] toPrimitiveLongArray()
      Converts this JSONArray to a primitive long array.
      Returns:
      a primitive long array
      Since:
      2.0.0
    • toPrimitiveFloatArray

      public float[] toPrimitiveFloatArray()
      Converts this JSONArray to a primitive float array.
      Returns:
      a primitive float array
      Since:
      2.0.0
    • toPrimitiveDoubleArray

      public double[] toPrimitiveDoubleArray()
      Converts this JSONArray to a primitive double array.
      Returns:
      a primitive double array
      Since:
      2.0.0
    • toPrimitiveBooleanArray

      public boolean[] toPrimitiveBooleanArray()
      Converts this JSONArray to a primitive boolean array.
      Returns:
      a primitive boolean array
      Since:
      2.0.0
    • toXML

      public String toXML(String rootName)
      Converts this JSONArray to a XML string.
      Parameters:
      rootName - name of the root element
      Returns:
      this JSON array as a XML string
      Since:
      1.1.0