Package com.alibaba.fastjson2
Class JSONArray
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<Object>,Collection<Object>,List<Object>,RandomAccess,SequencedCollection<Object>
- See Also:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddArray()Adds a newJSONObjectto the end of thisJSONArray.clone()Creates and returns a copy of thisJSONArray.static JSONArraycopyOf(Collection collection) Returns anJSONArraycontaining the elements of the given Collection, in its iteration order.Chained addition of elementsfluentAddAll(Collection<?> c) Chained add operation that appends all of the elements in the specified collection to the end of this list.Chained clear operation that removes all elements from thisJSONArray.fluentRemove(int index) Chained remove operation that removes the element at the specified position.Chained remove operation that removes the first occurrence of the specified element.fluentRemoveAll(Collection<?> c) Chained remove operation that removes from this list all of its elements that are contained in the specified collection.Chained set operation that replaces the element at the specified position.static JSONArraySeeJSON.toJSON(java.lang.Object)for detailsstatic JSONArrayfrom(Object obj, JSONWriter.Feature... writeFeatures) SeeJSON.toJSON(java.lang.Object)for detailsgetBigDecimal(int index) Returns theBigDecimalat the specified location in thisJSONArray.getBigInteger(int index) Returns theBigIntegerat the specified location in thisJSONArray.getBoolean(int index) booleangetBooleanValue(int index) Returns a boolean value at the specified location in thisJSONArray.getByte(int index) bytegetByteValue(int index) Returns a byte value at the specified location in thisJSONArray.getDate(int index) getDouble(int index) doublegetDoubleValue(int index) Returns a double value at the specified location in thisJSONArray.getFloat(int index) floatgetFloatValue(int index) Returns a float value at the specified location in thisJSONArray.getInstant(int index) getInteger(int index) intgetIntValue(int index) Returns an int value at the specified location in thisJSONArray.getJSONArray(int index) getJSONObject(int index) Returns theJSONObjectat the specified location in thisJSONArray.getLocalDate(int index) getLocalDate(int index, LocalDate defaultValue) getLocalDateTime(int index) Returns theLocalDateTimeat the specified location in thisJSONArray.getLocalDateTime(int index, LocalDateTime defaultValue) Returns theLocalDateTimeat the specified location in thisJSONArray.getLocalTime(int index) getLocalTime(int index, LocalTime defaultValue) getLong(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) getOffsetDateTime(int index) Returns theOffsetDateTimeat the specified location in thisJSONArray.getOffsetDateTime(int index, OffsetDateTime defaultValue) Returns theOffsetDateTimeat the specified location in thisJSONArray.getOffsetTime(int index) Returns theOffsetTimeat the specified location in thisJSONArray.getOffsetTime(int index, OffsetTime defaultValue) Returns theOffsetTimeat the specified location in thisJSONArray.getShort(int index) shortgetShortValue(int index) Returns a short value at the specified location in thisJSONArray.getString(int index) getZonedDateTime(int index) Returns theZonedDateTimeat the specified location in thisJSONArray.getZonedDateTime(int index, ZonedDateTime defaultValue) Returns theZonedDateTimeat the specified location in thisJSONArray.booleanisValid(JSONSchema schema) Checks if thisJSONArrayis valid against the specifiedJSONSchema.static JSONArrayPack an element asJSONArraystatic JSONArrayPack multiple elements asJSONArraystatic JSONArrayPack two elements asJSONArraystatic JSONArrayPack 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) Replaces the element at the specified position with the specified element<T> T<T> TConvert thisJSONArrayto the specified Object<T> TConvert 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.byte[]toJSONBBytes(JSONWriter.Feature... features) Serialize to JSONB bytestoJSONString(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.toString()Serialize to JSONStringtoString(JSONWriter.Feature... features) Serialize to JSONStringMethods inherited from class java.util.ArrayList
add, add, addAll, addAll, addFirst, addLast, clear, contains, ensureCapacity, equals, forEach, get, getFirst, getLast, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeFirst, removeIf, removeLast, removeRange, replaceAll, retainAll, size, sort, spliterator, subList, toArray, toArray, trimToSizeMethods inherited from class java.util.AbstractCollection
containsAllMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, stream, toArrayMethods inherited from interface java.util.List
containsAll, reversed
-
Constructor Details
-
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
- Parameters:
collection- the collection whose elements are to be placed into thisJSONArray- Throws:
NullPointerException- If the specified collection is null
-
JSONArray
- Parameters:
items- the array whose elements are to be placed into thisJSONArray- Throws:
NullPointerException- If the specified items is null
-
-
Method Details
-
set
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
- 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
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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
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
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
- Parameters:
index- index of the element to return- Returns:
Dateor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getDate
- Parameters:
index- index of the element to returndefaultValue- default value to return if the element is null- Returns:
Dateor defaultValue- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.27
-
getInstant
- Parameters:
index- index of the element to return- Returns:
Instantor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())
-
getLocalDate
- Parameters:
index- index of the element to return- Returns:
LocalDateor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getLocalDate
- Parameters:
index- index of the element to returndefaultValue- default value to return if the element is null- Returns:
LocalDateor defaultValue- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getLocalTime
- Parameters:
index- index of the element to return- Returns:
LocalTimeor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getLocalTime
- Parameters:
index- index of the element to returndefaultValue- default value to return if the element is null- Returns:
LocalTimeor defaultValue- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getOffsetTime
Returns theOffsetTimeat the specified location in thisJSONArray.- Parameters:
index- index of the element to return- Returns:
OffsetTimeor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getOffsetTime
Returns theOffsetTimeat the specified location in thisJSONArray.- Parameters:
index- index of the element to returndefaultValue- default value to return if the element is null- Returns:
OffsetTimeor defaultValue- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getLocalDateTime
Returns theLocalDateTimeat the specified location in thisJSONArray.- Parameters:
index- index of the element to return- Returns:
LocalDateTimeor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getLocalDateTime
Returns theLocalDateTimeat the specified location in thisJSONArray.- Parameters:
index- index of the element to returndefaultValue- default value to return if the element is null- Returns:
LocalDateTimeor defaultValue- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getOffsetDateTime
Returns theOffsetDateTimeat the specified location in thisJSONArray.- Parameters:
index- index of the element to return- Returns:
OffsetDateTimeor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getOffsetDateTime
Returns theOffsetDateTimeat the specified location in thisJSONArray.- Parameters:
index- index of the element to returndefaultValue- default value to return if the element is null- Returns:
OffsetDateTimeor defaultValue- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getZonedDateTime
Returns theZonedDateTimeat the specified location in thisJSONArray.- Parameters:
index- index of the element to return- Returns:
ZonedDateTimeor null- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
getZonedDateTime
Returns theZonedDateTimeat the specified location in thisJSONArray.- Parameters:
index- index of the element to returndefaultValue- default value to return if the element is null- Returns:
ZonedDateTimeor defaultValue- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0 || index >= size())- Since:
- 2.0.57
-
toString
Serialize to JSONString- Overrides:
toStringin classAbstractCollection<Object>- Returns:
- JSON
String
-
toString
Serialize to JSONString- Parameters:
features- features to be enabled in serialization- Returns:
- JSON
String
-
toJSONString
Serialize to JSONString- Parameters:
features- features to be enabled in serialization- Returns:
- JSON
String
-
toJSONString
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
Serialize to JSONB bytes- Parameters:
features- features to be enabled in serialization- Returns:
- JSONB bytes
-
to
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
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
- Since:
- 2.0.9
-
toJavaObject
Deprecated.since 2.0.4, please useto(Type)Convert thisJSONArrayto the specified Object- Parameters:
type- specify theTypeto be converted
-
toList
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
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
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
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
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
- Since:
- 2.0.3
-
addObject
Adds a newJSONObjectto the end of thisJSONArray.- Returns:
- the newly created
JSONObject - Since:
- 2.0.3
-
addArray
- Returns:
- the newly created
JSONArray - Since:
- 2.0.3
-
fluentAdd
Chained addition of elementsJSONArray array = new JSONArray().fluentAdd(1).fluentAdd(2).fluentAdd(3);
- Parameters:
element- element to be appended to this list- Returns:
- this
JSONArrayinstance - Since:
- 2.0.3
-
fluentClear
Chained clear operation that removes all elements from thisJSONArray.- Returns:
- this
JSONArrayinstance - Since:
- 2.0.3
-
fluentRemove
Chained remove operation that removes the element at the specified position.- Parameters:
index- the index of the element to be removed- Returns:
- this
JSONArrayinstance - Since:
- 2.0.3
-
fluentSet
Chained set operation that replaces the element at the specified position.- Parameters:
index- index of the element to replaceelement- element to be stored at the specified position- Returns:
- this
JSONArrayinstance - Since:
- 2.0.3
-
fluentRemove
Chained remove operation that removes the first occurrence of the specified element.- Parameters:
o- element to be removed from this list, if present- Returns:
- this
JSONArrayinstance - Since:
- 2.0.3
-
fluentRemoveAll
Chained remove operation that removes from this list all of its elements that are contained in the specified collection.- Parameters:
c- collection containing elements to be removed from this list- Returns:
- this
JSONArrayinstance - Since:
- 2.0.3
-
fluentAddAll
Chained add operation that appends all of the elements in the specified collection to the end of this list.- Parameters:
c- collection containing elements to be added to this list- Returns:
- this
JSONArrayinstance - Since:
- 2.0.3
-
isValid
Checks if thisJSONArrayis valid against the specifiedJSONSchema.- Parameters:
schema- theJSONSchemato validate against- Returns:
- true if this
JSONArrayis valid against the schema, false otherwise - Since:
- 2.0.3
-
clone
Creates and returns a copy of thisJSONArray. -
of
Pack multiple elements asJSONArrayJSONArray array = JSONArray.of(1, 2, "3", 4F, 5L, 6D, true);
- Parameters:
items- element set
-
of
Pack an element asJSONArrayJSONArray array = JSONArray.of("fastjson");- Parameters:
item- target element
-
copyOf
Returns anJSONArraycontaining the elements of the given Collection, in its iteration order.JSONArray array = JSONArray.copyOf(List.of("fastjson"));- Since:
- 2.0.22
-
of
Pack two elements asJSONArrayJSONArray array = JSONArray.of("fastjson", 2);- Parameters:
first- first elementsecond- second element
-
of
Pack three elements asJSONArrayJSONArray array = JSONArray.of("fastjson", 2, true);- Parameters:
first- first elementsecond- second elementthird- third element
-
parseArray
- Parameters:
text- the JSONStringto be parsedfeatures- features to be enabled in parsing
-
parseArray
-
parse
- Parameters:
text- the JSONStringto be parsedfeatures- features to be enabled in parsing- Since:
- 2.0.13
-
parseArray
-
from
SeeJSON.toJSON(java.lang.Object)for details -
from
SeeJSON.toJSON(java.lang.Object)for details
-
to(Type)