Package com.alibaba.fastjson2
Class JSONArray
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<Object>,Collection<Object>,List<Object>,RandomAccess
- See Also:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddArray()clone()Chained addition of elementsfluentAddAll(Collection<?> c) fluentRemove(int index) fluentRemoveAll(Collection<?> c) getBigDecimal(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.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) getShort(int index) shortgetShortValue(int index) Returns a short value at the specified location in thisJSONArray.getString(int index) booleanisValid(JSONSchema schema) 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) Replaces the element at the specified position with the specified element<T> T<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, 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, 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
-
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())
-
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())
-
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
- 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
-
addArray
-
fluentAdd
Chained addition of elementsJSONArray array = new JSONArray().fluentAdd(1).fluentAdd(2).fluentAdd(3);
- Parameters:
element- element to be appended to this list
-
fluentClear
- Since:
- 2.0.3
-
fluentRemove
- Since:
- 2.0.3
-
fluentSet
- Since:
- 2.0.3
-
fluentRemove
- Since:
- 2.0.3
-
fluentRemoveAll
- Since:
- 2.0.3
-
fluentAddAll
- Since:
- 2.0.3
-
isValid
- Since:
- 2.0.3
-
clone
-
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
-
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
-
parse
- Parameters:
text- the JSONStringto be parsedfeatures- features to be enabled in parsing- Since:
- 2.0.13
-
to(Type)