Package com.alibaba.fastjson2
Class JSONArray
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.ArrayList<Object>
-
- com.alibaba.fastjson2.JSONArray
-
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<Object>,Collection<Object>,List<Object>,RandomAccess
public class JSONArray extends ArrayList<Object>
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class java.util.AbstractList
modCount
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description JSONArrayaddArray()JSONObjectaddObject()Objectclone()static JSONArraycopyOf(Collection collection)Returns anJSONArraycontaining the elements of the given Collection, in its iteration order.JSONArrayfluentAdd(Object element)Chained addition of elementsJSONArrayfluentAddAll(Collection<?> c)JSONArrayfluentClear()JSONArrayfluentRemove(int index)JSONArrayfluentRemove(Object o)JSONArrayfluentRemoveAll(Collection<?> c)JSONArrayfluentSet(int index, Object element)static JSONArrayfrom(Object obj)SeeJSON.toJSON(java.lang.Object)for detailsstatic JSONArrayfrom(Object obj, JSONWriter.Feature... writeFeatures)SeeJSON.toJSON(java.lang.Object)for detailsBigDecimalgetBigDecimal(int index)Returns theBigDecimalat the specified location in thisJSONArray.BigIntegergetBigInteger(int index)Returns theBigIntegerat the specified location in thisJSONArray.BooleangetBoolean(int index)booleangetBooleanValue(int index)Returns a boolean value at the specified location in thisJSONArray.BytegetByte(int index)bytegetByteValue(int index)Returns a byte value at the specified location in thisJSONArray.DategetDate(int index)DategetDate(int index, Date defaultValue)DoublegetDouble(int index)doublegetDoubleValue(int index)Returns a double value at the specified location in thisJSONArray.FloatgetFloat(int index)floatgetFloatValue(int index)Returns a float value at the specified location in thisJSONArray.InstantgetInstant(int index)IntegergetInteger(int index)intgetIntValue(int index)Returns an int value at the specified location in thisJSONArray.JSONArraygetJSONArray(int index)JSONObjectgetJSONObject(int index)Returns theJSONObjectat the specified location in thisJSONArray.LonggetLong(int index)longgetLongValue(int index)Returns a long value at the specified location in thisJSONArray.<T> TgetObject(int index, Class<T> type, JSONReader.Feature... features)<T> TgetObject(int index, Type type, JSONReader.Feature... features)<T> TgetObject(int index, Function<JSONObject,T> creator)ShortgetShort(int index)shortgetShortValue(int index)Returns a short value at the specified location in thisJSONArray.StringgetString(int index)booleanisValid(JSONSchema schema)static JSONArrayof(Object item)Pack an element asJSONArraystatic JSONArrayof(Object... items)Pack multiple elements asJSONArraystatic JSONArrayof(Object first, Object second)Pack two elements asJSONArraystatic JSONArrayof(Object first, Object second, Object third)Pack three elements asJSONArraystatic JSONArrayparse(String text, JSONReader.Feature... features)static JSONArrayparseArray(String text, JSONReader.Feature... features)static <T> List<T>parseArray(String input, Class<T> type)static <T> List<T>parseArray(String text, Class<T> type, JSONReader.Feature... features)Objectset(int index, Object element)Replaces the element at the specified position with the specified element<T> Tto(Class<T> type)<T> Tto(Type type)Convert thisJSONArrayto the specified Object<T> Tto(Type type, long features)Convert thisJSONArrayto the specified Object<T> T[]toArray(Class<T> itemClass, JSONReader.Feature... features)Convert all the members of thisJSONArrayinto the specified Object.<T> List<T>toJavaList(Class<T> clazz, JSONReader.Feature... features)Convert all the members of thisJSONArrayinto the specified Object.<T> TtoJavaObject(Type type)Deprecated.since 2.0.4, please useto(Type)byte[]toJSONBBytes(JSONWriter.Feature... features)Serialize to JSONB bytesStringtoJSONString(JSONWriter.Feature... features)Serialize to JSONStringstatic StringtoJSONString(Object object, JSONWriter.Feature... features)Serialize Java Object to JSONStringwith specifiedJSONReader.Features enabled<T> List<T>toList(Class<T> itemClass, JSONReader.Feature... features)Convert all the members of thisJSONArrayinto the specified Object.StringtoString()Serialize to JSONStringStringtoString(JSONWriter.Feature... features)Serialize to JSONString-
Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, contains, ensureCapacity, equals, forEach, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, size, sort, spliterator, subList, toArray, toArray, trimToSize
-
Methods inherited from class java.util.AbstractCollection
containsAll
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, stream, toArray
-
Methods inherited from interface java.util.List
containsAll
-
-
-
-
Constructor Detail
-
JSONArray
public JSONArray()
default
-
JSONArray
public JSONArray(int initialCapacity)
- Parameters:
initialCapacity- the initial capacity of theJSONArray- 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 thisJSONArray- Throws:
NullPointerException- If the specified collection is null
-
JSONArray
public JSONArray(Object... items)
- Parameters:
items- the array whose elements are to be placed into thisJSONArray- 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 elementJSONArray 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]
-
getJSONArray
public JSONArray getJSONArray(int index)
- Parameters:
index- index of the element to return- Returns:
JSONArrayor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getJSONObject
public JSONObject getJSONObject(int index)
Returns theJSONObjectat the specified location in thisJSONArray.- Parameters:
index- index of the element to return- Returns:
JSONObjector null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getString
public String getString(int index)
- Parameters:
index- index of the element to return- Returns:
Stringor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getDouble
public Double getDouble(int index)
- Parameters:
index- index of the element to return- Returns:
Doubleor null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable doubleJSONException- Unsupported type conversion toDoubleIndexOutOfBoundsException- 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 thisJSONArray.- Parameters:
index- index of the element to return- Returns:
- double
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable doubleJSONException- Unsupported type conversion to double valueIndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getFloat
public Float getFloat(int index)
- Parameters:
index- index of the element to return- Returns:
Floator null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable floatJSONException- Unsupported type conversion toFloatIndexOutOfBoundsException- 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 thisJSONArray.- Parameters:
index- index of the element to return- Returns:
- float
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable floatJSONException- Unsupported type conversion to float valueIndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getLong
public Long getLong(int index)
- Parameters:
index- index of the element to return- Returns:
Longor null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable longJSONException- Unsupported type conversion toLongIndexOutOfBoundsException- 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 thisJSONArray.- Parameters:
index- index of the element to return- Returns:
- long
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable longJSONException- Unsupported type conversion to long valueIndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getInteger
public Integer getInteger(int index)
- Parameters:
index- index of the element to return- Returns:
Integeror null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable intJSONException- Unsupported type conversion toIntegerIndexOutOfBoundsException- 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 thisJSONArray.- Parameters:
index- index of the element to return- Returns:
- int
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable intJSONException- Unsupported type conversion to int valueIndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getShort
public Short getShort(int index)
- Parameters:
index- index of the element to return- Returns:
Shortor null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable shortJSONException- Unsupported type conversion toShortIndexOutOfBoundsException- 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 thisJSONArray.- Parameters:
index- index of the element to return- Returns:
- short
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable shortJSONException- Unsupported type conversion to short valueIndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getByte
public Byte getByte(int index)
- Parameters:
index- index of the element to return- Returns:
Byteor null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable byteJSONException- Unsupported type conversion toByteIndexOutOfBoundsException- 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 thisJSONArray.- Parameters:
index- index of the element to return- Returns:
- byte
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable byteJSONException- Unsupported type conversion to byte valueIndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getBoolean
public Boolean getBoolean(int index)
- Parameters:
index- index of the element to return- Returns:
Booleanor null- Throws:
JSONException- Unsupported type conversion toBooleanIndexOutOfBoundsException- 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 thisJSONArray.- Parameters:
index- index of the element to return- Returns:
- boolean
- Throws:
JSONException- Unsupported type conversion to boolean valueIndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getBigInteger
public BigInteger getBigInteger(int index)
Returns theBigIntegerat the specified location in thisJSONArray.- Parameters:
index- index of the element to return- Returns:
BigIntegeror null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())JSONException- Unsupported type conversion toBigIntegerNumberFormatException- If the value of get isStringand it is not a valid representation ofBigInteger
-
getBigDecimal
public BigDecimal getBigDecimal(int index)
Returns theBigDecimalat the specified location in thisJSONArray.- Parameters:
index- index of the element to return- Returns:
BigDecimalor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())JSONException- Unsupported type conversion toBigDecimalNumberFormatException- If the value of get isStringand it is not a valid representation ofBigDecimal
-
getDate
public Date getDate(int index)
- Parameters:
index- index of the element to return- Returns:
Dateor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getInstant
public Instant getInstant(int index)
- Parameters:
index- index of the element to return- Returns:
Instantor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
toString
public String toString()
Serialize to JSONString- Overrides:
toStringin classAbstractCollection<Object>- Returns:
- JSON
String
-
toString
public String toString(JSONWriter.Feature... features)
Serialize to JSONString- Parameters:
features- features to be enabled in serialization- Returns:
- JSON
String
-
toJSONString
public String toJSONString(JSONWriter.Feature... features)
Serialize to JSONString- 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 JSONStringwith specifiedJSONReader.Features enabled- Parameters:
object- Java Object to be serialized into JSONStringfeatures- 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 thisJSONArrayto the specified ObjectJSONArray array = ... List<User> users = array.to(new TypeReference<ArrayList<User>>(){}.getType());- Parameters:
type- specify theTypeto be converted- Since:
- 2.0.4
-
to
public <T> T to(Type type, long features)
Convert thisJSONArrayto the specified ObjectJSONArray array = ... List<User> users = array.to(new TypeReference<ArrayList<User>>(){}.getType());- Parameters:
type- specify theTypeto 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 useto(Type)Convert thisJSONArrayto the specified Object- Parameters:
type- specify theTypeto be converted
-
toList
public <T> List<T> toList(Class<T> itemClass, JSONReader.Feature... features)
Convert all the members of thisJSONArrayinto 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 theClass<T>to be convertedfeatures- 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 thisJSONArrayinto 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 theClass<T>to be convertedfeatures- 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 thisJSONArrayinto the specified Object.- Parameters:
clazz- specify theClass<T>to be convertedfeatures- features to be enabled in parsing please usetoList(Class, JSONReader.Feature...)
-
getObject
public <T> T getObject(int index, Type type, JSONReader.Feature... features)Returns the result of theTypeconverter conversion of the element at the specified position in thisJSONArray.JSONArray array = ... User user = array.getObject(0, TypeReference<HashMap<String ,User>>(){}.getType());- Parameters:
index- index of the element to returntype- specify theTypeto be converted- Returns:
<T>or null- Throws:
JSONException- If no suitable conversion method is foundIndexOutOfBoundsException- 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 theTypeconverter conversion of the element at the specified position in thisJSONArray.User user = jsonArray.getObject(0, User.class);- Parameters:
index- index of the element to returntype- specify theClassto be converted- Returns:
<T>or null- Throws:
JSONException- If no suitable conversion method is foundIndexOutOfBoundsException- 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 elementsJSONArray 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
-
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
-
of
public static JSONArray of(Object... items)
Pack multiple elements asJSONArrayJSONArray array = JSONArray.of(1, 2, "3", 4F, 5L, 6D, true);
- Parameters:
items- element set
-
of
public static JSONArray of(Object item)
Pack an element asJSONArrayJSONArray array = JSONArray.of("fastjson");- Parameters:
item- target element
-
copyOf
public static JSONArray copyOf(Collection collection)
Returns anJSONArraycontaining 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 asJSONArrayJSONArray array = JSONArray.of("fastjson", 2);- Parameters:
first- first elementsecond- second element
-
of
public static JSONArray of(Object first, Object second, Object third)
Pack three elements asJSONArrayJSONArray array = JSONArray.of("fastjson", 2, true);- Parameters:
first- first elementsecond- second elementthird- third element
-
parseArray
public static JSONArray parseArray(String text, JSONReader.Feature... features)
- Parameters:
text- the JSONStringto be parsedfeatures- features to be enabled in parsing
-
parseArray
public static <T> List<T> parseArray(String text, Class<T> type, JSONReader.Feature... features)
-
parse
public static JSONArray parse(String text, JSONReader.Feature... features)
- Parameters:
text- the JSONStringto be parsedfeatures- features to be enabled in parsing- Since:
- 2.0.13
-
from
public static JSONArray from(Object obj)
SeeJSON.toJSON(java.lang.Object)for details
-
from
public static JSONArray from(Object obj, JSONWriter.Feature... writeFeatures)
SeeJSON.toJSON(java.lang.Object)for details
-
-