- 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
ConstructorDescriptionConstructs 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 TypeMethodDescriptionboolean
getArray
(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, itsbyte
value 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, itsdouble
value 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, itsfloat
value is returned.getInteger
(int index) If the value is a Number, itsint
value is returned.getLong
(int index) If the value is a Number, itslong
value 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, itsshort
value 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 primitiveboolean
array.byte[]
Converts this JSONArray to a primitivebyte
array.double[]
Converts this JSONArray to a primitivedouble
array.float[]
Converts this JSONArray to a primitivefloat
array.int[]
Converts this JSONArray to a primitiveint
array.long[]
Converts this JSONArray to a primitivelong
array.short[]
Converts this JSONArray to a primitiveshort
array.Short[]
toString()
Converts this JSONArray to a JSON string.String[]
Converts this JSONArray to a XML string.void
Methods 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, trimToSize
Methods inherited from class java.util.AbstractCollection
containsAll
Methods inherited from interface java.util.Collection
parallelStream, stream, toArray
Methods 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
-true
for strict validation of JSON data;false
if 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
-true
for strict validation of JSON data;false
if 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:
toString
in 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
JSONObject
ornull
- 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 returntrue
in case itslong
value is1
. It will returnfalse
if it is a number but thelong
value 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, itsbyte
value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1
in case the value istrue
otherwise0
. 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, itsshort
value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1
in case the value istrue
otherwise0
. 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, itsint
value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1
in case the value istrue
otherwise0
. 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, itslong
value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1
in case the value istrue
otherwise0
. 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, itsfloat
value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1
in case the value istrue
otherwise0
. 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, itsdouble
value is returned. If the value is a String, it will be parsed. The result is returned. If the value is a Boolean, this method returns1
in case the value istrue
otherwise0
. 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 primitivebyte
array.- Returns:
- a primitive
byte
array - Since:
- 2.0.0
-
toPrimitiveShortArray
public short[] toPrimitiveShortArray()Converts this JSONArray to a primitiveshort
array.- Returns:
- a primitive
short
array - Since:
- 2.0.0
-
toPrimitiveIntArray
public int[] toPrimitiveIntArray()Converts this JSONArray to a primitiveint
array.- Returns:
- a primitive
int
array - Since:
- 2.0.0
-
toPrimitiveLongArray
public long[] toPrimitiveLongArray()Converts this JSONArray to a primitivelong
array.- Returns:
- a primitive
long
array - Since:
- 2.0.0
-
toPrimitiveFloatArray
public float[] toPrimitiveFloatArray()Converts this JSONArray to a primitivefloat
array.- Returns:
- a primitive
float
array - Since:
- 2.0.0
-
toPrimitiveDoubleArray
public double[] toPrimitiveDoubleArray()Converts this JSONArray to a primitivedouble
array.- Returns:
- a primitive
double
array - Since:
- 2.0.0
-
toPrimitiveBooleanArray
public boolean[] toPrimitiveBooleanArray()Converts this JSONArray to a primitiveboolean
array.- Returns:
- a primitive
boolean
array - 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
-