Class JSONArray

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

public class JSONArray extends ArrayList<Object>
See Also:
  • Constructor Details

    • JSONArray

      public JSONArray()
      default
    • JSONArray

      public JSONArray(int initialCapacity)
      Parameters:
      initialCapacity - the initial capacity of the JSONArray
      Throws:
      IllegalArgumentException - If the specified initial capacity is negative
    • JSONArray

      public JSONArray(Collection<?> collection)
      Parameters:
      collection - the collection whose elements are to be placed into this JSONArray
      Throws:
      NullPointerException - If the specified collection is null
    • JSONArray

      public JSONArray(Object... items)
      Parameters:
      items - the array whose elements are to be placed into this JSONArray
      Throws:
      NullPointerException - If the specified items is null
  • Method Details

    • set

      public Object set(int index, Object element)
      Replaces the element at the specified position with the specified element
      
          JSONArray array = new JSONArray();
          array.add(-1); // [-1]
          array.add(2); // [-1,2]
          array.set(0, 1); // [1,2]
          array.set(4, 3); // [1,2,null,null,3]
          array.set(-1, -1); // [1,2,null,null,-1]
          array.set(-2, -2); // [1,2,null,-2,-1]
          array.set(-6, -6); // [-6,1,2,null,-2,-1]
       
      Specified by:
      set in interface List<Object>
      Overrides:
      set in class ArrayList<Object>
      Parameters:
      index - index of the element to replace
      element - element to be stored at the specified position
      Returns:
      the element previously at the specified position
      Since:
      2.0.3
    • getJSONArray

      public JSONArray getJSONArray(int index)
      Returns the JSONArray at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      JSONArray or null
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getJSONObject

      public JSONObject getJSONObject(int index)
      Returns the JSONObject at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      JSONObject or null
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getString

      public String getString(int index)
      Returns the String at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      String or null
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getDouble

      public Double getDouble(int index)
      Returns the Double at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      Double or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable double
      JSONException - Unsupported type conversion to Double
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getDoubleValue

      public double getDoubleValue(int index)
      Returns a double value at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      double
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable double
      JSONException - Unsupported type conversion to double value
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getFloat

      public Float getFloat(int index)
      Returns the Float at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      Float or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable float
      JSONException - Unsupported type conversion to Float
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getFloatValue

      public float getFloatValue(int index)
      Returns a float value at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      float
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable float
      JSONException - Unsupported type conversion to float value
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getLong

      public Long getLong(int index)
      Returns the Long at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      Long or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable long
      JSONException - Unsupported type conversion to Long
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getLongValue

      public long getLongValue(int index)
      Returns a long value at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      long
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable long
      JSONException - Unsupported type conversion to long value
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getInteger

      public Integer getInteger(int index)
      Returns the Integer at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      Integer or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable int
      JSONException - Unsupported type conversion to Integer
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getIntValue

      public int getIntValue(int index)
      Returns an int value at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      int
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable int
      JSONException - Unsupported type conversion to int value
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getShort

      public Short getShort(int index)
      Returns the Short at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      Short or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable short
      JSONException - Unsupported type conversion to Short
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getShortValue

      public short getShortValue(int index)
      Returns a short value at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      short
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable short
      JSONException - Unsupported type conversion to short value
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getByte

      public Byte getByte(int index)
      Returns the Byte at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      Byte or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable byte
      JSONException - Unsupported type conversion to Byte
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getByteValue

      public byte getByteValue(int index)
      Returns a byte value at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      byte
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable byte
      JSONException - Unsupported type conversion to byte value
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getBoolean

      public Boolean getBoolean(int index)
      Returns the Boolean at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      Boolean or null
      Throws:
      JSONException - Unsupported type conversion to Boolean
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getBooleanValue

      public boolean getBooleanValue(int index)
      Returns a boolean value at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      boolean
      Throws:
      JSONException - Unsupported type conversion to boolean value
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getBigInteger

      public BigInteger getBigInteger(int index)
      Returns the BigInteger at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      BigInteger or null
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
      JSONException - Unsupported type conversion to BigInteger
      NumberFormatException - If the value of get is String and it is not a valid representation of BigInteger
    • getBigDecimal

      public BigDecimal getBigDecimal(int index)
      Returns the BigDecimal at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      BigDecimal or null
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
      JSONException - Unsupported type conversion to BigDecimal
      NumberFormatException - If the value of get is String and it is not a valid representation of BigDecimal
    • getDate

      public Date getDate(int index)
      Returns the Date at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      Date or null
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getDate

      public Date getDate(int index, Date defaultValue)
      Since:
      2.0.27
    • getInstant

      public Instant getInstant(int index)
      Returns the Instant at the specified location in this JSONArray.
      Parameters:
      index - index of the element to return
      Returns:
      Instant or null
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • toString

      public String toString()
      Serialize to JSON String
      Overrides:
      toString in class AbstractCollection<Object>
      Returns:
      JSON String
    • toString

      public String toString(JSONWriter.Feature... features)
      Serialize to JSON String
      Parameters:
      features - features to be enabled in serialization
      Returns:
      JSON String
    • toJSONString

      public String toJSONString(JSONWriter.Feature... features)
      Serialize to JSON String
      Parameters:
      features - features to be enabled in serialization
      Returns:
      JSON String
    • toJSONString

      public static String toJSONString(Object object, JSONWriter.Feature... features)
      Serialize Java Object to JSON String with specified JSONReader.Features enabled
      Parameters:
      object - Java Object to be serialized into JSON String
      features - features to be enabled in serialization
      Since:
      2.0.15
    • toJSONBBytes

      public byte[] toJSONBBytes(JSONWriter.Feature... features)
      Serialize to JSONB bytes
      Parameters:
      features - features to be enabled in serialization
      Returns:
      JSONB bytes
    • to

      public <T> T to(Type type)
      Convert this JSONArray to the specified Object
      
       JSONArray array = ...
       List<User> users = array.to(new TypeReference<ArrayList<User>>(){}.getType());
       
      Parameters:
      type - specify the Type to be converted
      Since:
      2.0.4
    • to

      public <T> T to(Type type, long features)
      Convert this JSONArray to the specified Object
      
       JSONArray array = ...
       List<User> users = array.to(new TypeReference<ArrayList<User>>(){}.getType());
       
      Parameters:
      type - specify the Type to be converted
      Since:
      2.0.51
    • to

      public <T> T to(Class<T> type)
      Since:
      2.0.9
    • toJavaObject

      @Deprecated public <T> T toJavaObject(Type type)
      Deprecated.
      since 2.0.4, please use to(Type)
      Convert this JSONArray to the specified Object
      Parameters:
      type - specify the Type to be converted
    • toList

      public <T> List<T> toList(Class<T> itemClass, JSONReader.Feature... features)
      Convert all the members of this JSONArray into the specified Object.
      
       String json = "[{\"id\": 1, \"name\": \"fastjson\"}, {\"id\": 2, \"name\": \"fastjson2\"}]";
       JSONArray array = JSON.parseArray(json);
       List<User> users = array.toList(User.class);
       
      Parameters:
      itemClass - specify the Class<T> to be converted
      features - features to be enabled in parsing
      Since:
      2.0.4
    • toArray

      public <T> T[] toArray(Class<T> itemClass, JSONReader.Feature... features)
      Convert all the members of this JSONArray into the specified Object.
      
       String json = "[{\"id\": 1, \"name\": \"fastjson\"}, {\"id\": 2, \"name\": \"fastjson2\"}]";
       JSONArray array = JSON.parseArray(json);
       List<User> users = array.toList(User.class);
       
      Parameters:
      itemClass - specify the Class<T> to be converted
      features - features to be enabled in parsing
      Since:
      2.0.4
    • toJavaList

      public <T> List<T> toJavaList(Class<T> clazz, JSONReader.Feature... features)
      Convert all the members of this JSONArray into the specified Object.
      Parameters:
      clazz - specify the Class<T> to be converted
      features - features to be enabled in parsing please use toList(Class, JSONReader.Feature...)
    • getObject

      public <T> T getObject(int index, Type type, JSONReader.Feature... features)
      Returns the result of the Type converter conversion of the element at the specified position in this JSONArray.
      
       JSONArray array = ...
       User user = array.getObject(0, TypeReference<HashMap<String ,User>>(){}.getType());
       
      Parameters:
      index - index of the element to return
      type - specify the Type to be converted
      Returns:
      <T> or null
      Throws:
      JSONException - If no suitable conversion method is found
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getObject

      public <T> T getObject(int index, Class<T> type, JSONReader.Feature... features)
      Returns the result of the Type converter conversion of the element at the specified position in this JSONArray.

      User user = jsonArray.getObject(0, User.class);

      Parameters:
      index - index of the element to return
      type - specify the Class to be converted
      Returns:
      <T> or null
      Throws:
      JSONException - If no suitable conversion method is found
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • getObject

      public <T> T getObject(int index, Function<JSONObject,T> creator)
      Since:
      2.0.3
    • addObject

      public JSONObject addObject()
    • addArray

      public JSONArray addArray()
    • fluentAdd

      public JSONArray fluentAdd(Object element)
      Chained addition of elements
       JSONArray array = new JSONArray().fluentAdd(1).fluentAdd(2).fluentAdd(3);
       
      Parameters:
      element - element to be appended to this list
    • fluentClear

      public JSONArray fluentClear()
      Since:
      2.0.3
    • fluentRemove

      public JSONArray fluentRemove(int index)
      Since:
      2.0.3
    • fluentSet

      public JSONArray fluentSet(int index, Object element)
      Since:
      2.0.3
    • fluentRemove

      public JSONArray fluentRemove(Object o)
      Since:
      2.0.3
    • fluentRemoveAll

      public JSONArray fluentRemoveAll(Collection<?> c)
      Since:
      2.0.3
    • fluentAddAll

      public JSONArray fluentAddAll(Collection<?> c)
      Since:
      2.0.3
    • isValid

      public boolean isValid(JSONSchema schema)
      Since:
      2.0.3
    • clone

      public Object clone()
      Overrides:
      clone in class ArrayList<Object>
    • of

      public static JSONArray of(Object... items)
      Pack multiple elements as JSONArray
       JSONArray array = JSONArray.of(1, 2, "3", 4F, 5L, 6D, true);
       
      Parameters:
      items - element set
    • of

      public static JSONArray of(Object item)
      Pack an element as JSONArray
       JSONArray array = JSONArray.of("fastjson");
       
      Parameters:
      item - target element
    • copyOf

      public static JSONArray copyOf(Collection collection)
      Returns an JSONArray containing the elements of the given Collection, in its iteration order.
       JSONArray array = JSONArray.copyOf(List.of("fastjson"));
       
      Since:
      2.0.22
    • of

      public static JSONArray of(Object first, Object second)
      Pack two elements as JSONArray
       JSONArray array = JSONArray.of("fastjson", 2);
       
      Parameters:
      first - first element
      second - second element
    • of

      public static JSONArray of(Object first, Object second, Object third)
      Pack three elements as JSONArray
       JSONArray array = JSONArray.of("fastjson", 2, true);
       
      Parameters:
      first - first element
      second - second element
      third - third element
    • parseArray

      public static JSONArray parseArray(String text, JSONReader.Feature... features)
      Parse JSON String into JSONArray
      Parameters:
      text - the JSON String to be parsed
      features - features to be enabled in parsing
    • parseArray

      public static <T> List<T> parseArray(String text, Class<T> type, JSONReader.Feature... features)
      Parse JSON String into List
      Parameters:
      text - the JSON String to be parsed
      type - specify the Class to be converted
      features - features to be enabled in parsing
    • parse

      public static JSONArray parse(String text, JSONReader.Feature... features)
      Parse JSON String into JSONArray
      Parameters:
      text - the JSON String to be parsed
      features - features to be enabled in parsing
      Since:
      2.0.13
    • parseArray

      public static <T> List<T> parseArray(String input, Class<T> type)
      Parse JSON String into List
      Parameters:
      input - the JSON String to be parsed
      type - specify the Class to be converted
      Since:
      2.0.24
    • from

      public static JSONArray from(Object obj)
    • from

      public static JSONArray from(Object obj, JSONWriter.Feature... writeFeatures)