- All Implemented Interfaces:
Serializable,Cloneable,Iterable<Object>,Collection<Object>,List<Object>,RandomAccess
Represents a JSON array.
- Since:
- 1.0.0
- Version:
- 2.1.0
- Author:
- FangYidong(fangyidong@yahoo.com.cn), Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty JSONArray.JSONArray(boolean[] array) Constructs a JSONArray with the elements of the given array.JSONArray(byte[] array) Constructs a JSONArray with the elements of the given array.JSONArray(char[] array) Constructs a JSONArray with the elements of the given array.JSONArray(double[] array) Constructs a JSONArray with the elements of the given array.JSONArray(float[] array) Constructs a JSONArray with the elements of the given array.JSONArray(int[] array) Constructs a JSONArray with the elements of the given array.JSONArray(long[] array) Constructs a JSONArray with the elements of the given array.JSONArray(short[] array) Constructs a JSONArray with the elements of the given array.Constructs a JSONArray with the elements of the given array.Constructs a JSONArray from a JSON string.Constructs a JSONArray from a JSON string.JSONArray(Collection<?> collection) Constructs a JSONArray with the elements of the given Collection.JSONArray(T[] array) Constructs a JSONArray with the elements of the given array. -
Method Summary
Modifier and TypeMethodDescriptionbooleangetArray(int index) If the value already is a JSONArray, it will be casted and returned.getBoolean(int index) If the value is already a Boolean, it will be casted and returned.getByte(int index) If the value is a Number, itsbytevalue is returned.getDate(int index, DateFormat format) If the value already is a Date, it will be casted and returned.getDouble(int index) If the value is a Number, itsdoublevalue is returned.<T extends Enum<T>>
TIf the String representation of the value equals the name of the enum constant in the given enum type, it will return the enum constant.getFloat(int index) If the value is a Number, itsfloatvalue is returned.getInteger(int index) If the value is a Number, itsintvalue is returned.getLong(int index) If the value is a Number, itslongvalue is returned.getObject(int index) If the value is a JSONObject already, it will be casted and returned.getShort(int index) If the value is a Number, itsshortvalue is returned.getString(int index) Returns the same as the value'sObject.toString()method.Boolean[]Byte[]Double[]Float[]Integer[]Long[]boolean[]Converts this JSONArray to a primitivebooleanarray.byte[]Converts this JSONArray to a primitivebytearray.double[]Converts this JSONArray to a primitivedoublearray.float[]Converts this JSONArray to a primitivefloatarray.int[]Converts this JSONArray to a primitiveintarray.long[]Converts this JSONArray to a primitivelongarray.short[]Converts this JSONArray to a primitiveshortarray.Short[]toString()Converts this JSONArray to a JSON string.String[]Converts this JSONArray to a XML string.voidMethods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, clone, contains, ensureCapacity, forEach, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray, trimToSizeMethods inherited from class java.util.AbstractCollection
containsAllMethods inherited from interface java.util.Collection
parallelStream, stream, toArrayMethods inherited from interface java.util.List
containsAll
-
Constructor Details
-
JSONArray
public JSONArray()Constructs an empty JSONArray.- Since:
- 1.0.0
-
JSONArray
Constructs a JSONArray with the elements of the given Collection.- Parameters:
collection- the Collection- Since:
- 1.0.0
-
JSONArray
public JSONArray(T[] array) Constructs a JSONArray with the elements of the given array.- Type Parameters:
T- the array type- Parameters:
array- the array- Since:
- 1.0.0
-
JSONArray
public JSONArray(byte[] array) Constructs a JSONArray with the elements of the given array.- Parameters:
array- the array- Since:
- 1.0.0
-
JSONArray
public JSONArray(boolean[] array) Constructs a JSONArray with the elements of the given array.- Parameters:
array- the array- Since:
- 1.0.0
-
JSONArray
public JSONArray(char[] array) Constructs a JSONArray with the elements of the given array.- Parameters:
array- the array- Since:
- 1.0.0
-
JSONArray
public JSONArray(short[] array) Constructs a JSONArray with the elements of the given array.- Parameters:
array- the array- Since:
- 1.0.0
-
JSONArray
public JSONArray(int[] array) Constructs a JSONArray with the elements of the given array.- Parameters:
array- the array- Since:
- 1.0.0
-
JSONArray
public JSONArray(long[] array) Constructs a JSONArray with the elements of the given array.- Parameters:
array- the array- Since:
- 1.0.0
-
JSONArray
public JSONArray(float[] array) Constructs a JSONArray with the elements of the given array.- Parameters:
array- the array- Since:
- 1.0.0
-
JSONArray
public JSONArray(double[] array) Constructs a JSONArray with the elements of the given array.- Parameters:
array- the array- Since:
- 1.0.0
-
JSONArray
Constructs a JSONArray with the elements of the given array. This constructor is a little slower than the others because it uses the Reflection API.- Parameters:
array- the array- Since:
- 1.0.0
-
JSONArray
Constructs a JSONArray from a JSON string.- Parameters:
json- the JSON string- Throws:
JSONParseException- if the JSON is invalid- Since:
- 1.0.0
-
JSONArray
Constructs a JSONArray from a JSON string.- Parameters:
json- the JSON stringstrict-truefor strict validation of JSON data;falseif missing colons and commas should be tolerated- Throws:
JSONParseException- if the JSON is invalid- Since:
- 2.1.0
-
JSONArray
- Parameters:
reader- the Reader- Throws:
JSONParseException- if the JSON is invalidIOException- if an I/O error occurred- Since:
- 1.0.0
-
JSONArray
- Parameters:
reader- the Readerstrict-truefor strict validation of JSON data;falseif missing colons and commas should be tolerated- Throws:
JSONParseException- if the JSON is invalidIOException- if an I/O error occurred- Since:
- 2.1.0
-
-
Method Details
-
write
- Parameters:
writer- the Writer- Throws:
IOException- if an I/O error occurred- Since:
- 1.0.0
-
toString
Converts this JSONArray to a JSON string.- Overrides:
toStringin classAbstractCollection<Object>- Returns:
- this JSONArray as a JSON string
- Since:
- 1.0.0
-
equals
-
getObject
If the value is a JSONObject already, it will be casted and returned. If the value is a Map, it will be wrapped in a JSONObject. The wrapped JSONObject will be returned. In any other case this method returnsnull.- Parameters:
index- index of the value- Returns:
- a
JSONObjectornull - Since:
- 1.0.0
-
getArray
If the value already is a JSONArray, it will be casted and returned. If the value is an array or Collection, it will be wrapped in a JSONArray. The JSONArray will be returned. In any other case this method returnsnull.- Parameters:
index- index of the value- Returns:
- a JSONArray or
null - Since:
- 1.0.0
-
getBoolean
If the value is already a Boolean, it will be casted and returned. If the value is a String, it will be parsed. The result is returned. If the value is a Number, this method will returntruein case itslongvalue is1. It will returnfalseif it is a number but thelongvalue is not1. In any other case this method returnsnull.- Parameters:
index- index of the value- Returns:
- a Boolean or
null - Since:
- 1.0.0
-
getByte
If the value is a Number, itsbytevalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
index- index of the value- Returns:
- a Byte or
null - Since:
- 1.0.0
-
getShort
If the value is a Number, itsshortvalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
index- index of the value- Returns:
- a Short or
null - Since:
- 1.0.0
-
getInteger
If the value is a Number, itsintvalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
index- index of the value- Returns:
- a Integer or
null - Since:
- 1.0.0
-
getLong
If the value is a Number, itslongvalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
index- index of the value- Returns:
- a Long or
null - Since:
- 1.0.0
-
getFloat
If the value is a Number, itsfloatvalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
index- index of the value- Returns:
- a Float or
null - Since:
- 1.0.0
-
getDouble
If the value is a Number, itsdoublevalue is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1in case the value istrueotherwise0. In any other case this method returnsnull.- Parameters:
index- index of the value- Returns:
- a Double or
null - Since:
- 1.0.0
-
getString
Returns the same as the value'sObject.toString()method. If the actual value isnull, this method will returnnull.- Parameters:
index- index of the value- Returns:
- a String or
null - Since:
- 1.0.0
-
getDate
If the value already is a Date, it will be casted and returned. Otherwise the result of the value'sObject.toString()will be parsed by the given DateFormat. The result is returned. If the actual value isnull, this method will returnnull.- Parameters:
index- index of the valueformat- the DateFormat used to parse the date- Returns:
- a Date or
null - Throws:
ParseException- if the date could not be parsed- Since:
- 1.0.0
-
getEnum
If the String representation of the value equals the name of the enum constant in the given enum type, it will return the enum constant. In any other case this method returnsnull.- Type Parameters:
T- the return type- Parameters:
index- index of the valuetype- the enum type- Returns:
- an Enum or
null - Since:
- 1.0.0
-
toBooleanArray
- Returns:
- an array Booleans
- Since:
- 1.0.0
-
toByteArray
- Returns:
- an array of Bytes
- Since:
- 1.0.0
-
toShortArray
- Returns:
- an array of Shorts
- Since:
- 1.0.0
-
toIntegerArray
- Returns:
- an array of Integers
- Since:
- 1.0.0
-
toLongArray
- Returns:
- an array of Longs
- Since:
- 1.0.0
-
toFloatArray
- Returns:
- an array of Floats
- Since:
- 1.0.0
-
toDoubleArray
- Returns:
- an array of Doubles
- Since:
- 1.0.0
-
toStringArray
- Returns:
- an array of Strings
- Since:
- 1.0.0
-
toObjectArray
- Returns:
- an array of JSONObjects
- Since:
- 1.0.0
-
toArrayArray
- Returns:
- an array of JSONArrays.
- Since:
- 1.0.0
-
toPrimitiveByteArray
public byte[] toPrimitiveByteArray()Converts this JSONArray to a primitivebytearray.- Returns:
- a primitive
bytearray - Since:
- 2.0.0
-
toPrimitiveShortArray
public short[] toPrimitiveShortArray()Converts this JSONArray to a primitiveshortarray.- Returns:
- a primitive
shortarray - Since:
- 2.0.0
-
toPrimitiveIntArray
public int[] toPrimitiveIntArray()Converts this JSONArray to a primitiveintarray.- Returns:
- a primitive
intarray - Since:
- 2.0.0
-
toPrimitiveLongArray
public long[] toPrimitiveLongArray()Converts this JSONArray to a primitivelongarray.- Returns:
- a primitive
longarray - Since:
- 2.0.0
-
toPrimitiveFloatArray
public float[] toPrimitiveFloatArray()Converts this JSONArray to a primitivefloatarray.- Returns:
- a primitive
floatarray - Since:
- 2.0.0
-
toPrimitiveDoubleArray
public double[] toPrimitiveDoubleArray()Converts this JSONArray to a primitivedoublearray.- Returns:
- a primitive
doublearray - Since:
- 2.0.0
-
toPrimitiveBooleanArray
public boolean[] toPrimitiveBooleanArray()Converts this JSONArray to a primitivebooleanarray.- Returns:
- a primitive
booleanarray - Since:
- 2.0.0
-
toXML
Converts this JSONArray to a XML string.- Parameters:
rootName- name of the root element- Returns:
- this JSON array as a XML string
- Since:
- 1.1.0
-