Class JSONArray

    • Constructor Detail

      • 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 Detail

      • 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
      • 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())
      • 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())
      • 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())
      • 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())
      • 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())
      • 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())
      • 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())
      • 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())
      • 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​(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
      • 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
      • 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
      • isValid

        public boolean isValid​(JSONSchema schema)
        Since:
        2.0.3
      • 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 <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
      • 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