public interface JsonArray extends JsonStructure, java.util.List<JsonValue>
JsonArray
represents an immutable JSON array
(an ordered sequence of zero or more values).
It also provides an unmodifiable list view of the values in the array.
A JsonArray
object can be created by reading JSON data from
an input source or it can be built from scratch using an array builder
object.
The following example demonstrates how to create a JsonArray
object from an input source using the method JsonReader.readArray()
:
JsonReader jsonReader = Json.createReader(...);
JsonArray array = jsonReader.readArray();
jsonReader.close();
The following example demonstrates how to build an empty JSON array
using the class JsonArrayBuilder
:
JsonArray array = Json.createArrayBuilder().build();
The example code below demonstrates how to create the following JSON array:
[
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
JsonArray value = Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("type", "home")
.add("number", "212 555-1234"))
.add(Json.createObjectBuilder()
.add("type", "fax")
.add("number", "646 555-4567"))
.build();
The following example demonstrates how to write a JsonArray
object
as JSON data:
JsonArray arr = ...;
JsonWriter writer = Json.createWriter(...)
writer.writeArray(arr);
writer.close();
The values in a JsonArray
can be of the following types:
JsonObject
, JsonArray
,
JsonString
, JsonNumber
, JsonValue.TRUE
,
JsonValue.FALSE
, and JsonValue.NULL
.
JsonArray
provides various accessor methods to access the values
in an array.
The following example shows how to obtain the home phone number "212 555-1234" from the array built in the previous example:
JsonObject home = array.getJsonObject(0);
String number = home.getString("number");
JsonArray
instances are list objects that provide read-only
access to the values in the JSON array. Any attempt to modify the list,
whether directly or using its collection views, results in an
UnsupportedOperationException
.
JsonValue.ValueType
EMPTY_JSON_ARRAY, EMPTY_JSON_OBJECT, FALSE, NULL, TRUE
Modifier and Type | Method and Description |
---|---|
boolean |
getBoolean(int index)
Returns the boolean value at the specified position.
|
boolean |
getBoolean(int index,
boolean defaultValue)
Returns the boolean value at the specified position.
|
int |
getInt(int index)
A convenience method for
getJsonNumber(index).intValue() . |
int |
getInt(int index,
int defaultValue)
Returns the int value of the
JsonNumber at the specified position. |
JsonArray |
getJsonArray(int index)
Returns the array value at the specified position in this array.
|
JsonNumber |
getJsonNumber(int index)
Returns the number value at the specified position in this array.
|
JsonObject |
getJsonObject(int index)
Returns the object value at the specified position in this array.
|
JsonString |
getJsonString(int index)
Returns the string value at ths specified position in this array.
|
java.lang.String |
getString(int index)
A convenience method for
getJsonString(index).getString() . |
java.lang.String |
getString(int index,
java.lang.String defaultValue)
Returns the
String value of JsonString at the specified
position in this JSON array values. |
<T extends JsonValue> |
getValuesAs(java.lang.Class<T> clazz)
Returns a list view of the specified type for the array.
|
default <T,K extends JsonValue> |
getValuesAs(java.util.function.Function<K,T> func)
Returns a list view for the array.
|
boolean |
isNull(int index)
Returns
true if the value at the specified location in this
array is JsonValue.NULL . |
getValue
asJsonArray, asJsonObject, getValueType, toString
add, add, addAll, addAll, clear, contains, containsAll, equals, get, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, size, sort, spliterator, subList, toArray, toArray
JsonObject getJsonObject(int index)
(JsonObject)get(index)
.index
- index of the value to be returnedjava.lang.IndexOutOfBoundsException
- if the index is out of rangejava.lang.ClassCastException
- if the value at the specified position is not
assignable to the JsonObject typeJsonArray getJsonArray(int index)
(JsonArray)get(index)
.index
- index of the value to be returnedjava.lang.IndexOutOfBoundsException
- if the index is out of rangejava.lang.ClassCastException
- if the value at the specified position is not
assignable to the JsonArray typeJsonNumber getJsonNumber(int index)
(JsonNumber)get(index)
.index
- index of the value to be returnedjava.lang.IndexOutOfBoundsException
- if the index is out of rangejava.lang.ClassCastException
- if the value at the specified position is not
assignable to the JsonNumber typeJsonString getJsonString(int index)
(JsonString)get(index)
.index
- index of the value to be returnedjava.lang.IndexOutOfBoundsException
- if the index is out of rangejava.lang.ClassCastException
- if the value at the specified position is not
assignable to the JsonString type<T extends JsonValue> java.util.List<T> getValuesAs(java.lang.Class<T> clazz)
ClassCastException
, if there is a value of wrong type in this
array. Unfortunately, the exception can occur at any time after this
method returns.T
- The type of the List for the arrayclazz
- a JsonValue typedefault <T,K extends JsonValue> java.util.List<T> getValuesAs(java.util.function.Function<K,T> func)
func
argument.
This method can be used to obtain a list of the unwrapped types, such as
List<String> strings = ary1.getValuesAs(JsonString::getString);
List<Integer> ints = ary2.getValuesAs(JsonNumber::intValue);
or a list of simple projections, such as
List<Integer> stringsizes = ary1.getValueAs((JsonString v)->v.getString().length();
K
- The element type (must be a subtype of JsonValue) of this JsonArray.T
- The element type of the returned Listfunc
- The function that maps the elements of this JsonArray to the target elements.java.lang.ClassCastException
- if the JsonArray
contains a value of wrong typejava.lang.String getString(int index)
getJsonString(index).getString()
.index
- index of the JsonString
valuejava.lang.IndexOutOfBoundsException
- if the index is out of rangejava.lang.ClassCastException
- if the value at the specified position is not
assignable to JsonString
java.lang.String getString(int index, java.lang.String defaultValue)
String
value of JsonString
at the specified
position in this JSON array values. If JsonString
is found,
its JsonString.getString()
is returned. Otherwise,
the specified default value is returned.index
- index of the JsonString
valuedefaultValue
- the String to return if the JsonValue
at the
specified position is not a JsonString
int getInt(int index)
getJsonNumber(index).intValue()
.index
- index of the JsonNumber
valuejava.lang.IndexOutOfBoundsException
- if the index is out of rangejava.lang.ClassCastException
- if the value at the specified position is not
assignable to JsonNumber
int getInt(int index, int defaultValue)
JsonNumber
at the specified position.
If the value at that position is a JsonNumber
,
this method returns JsonNumber.intValue()
. Otherwise
this method returns the specified default value.index
- index of the JsonNumber
valuedefaultValue
- the int value to return if the JsonValue
at
the specified position is not a JsonNumber
boolean getBoolean(int index)
JsonValue.TRUE
this method returns true
. If the value at the specified position
is JsonValue.FALSE
this method returns false
.index
- index of the JSON boolean valuejava.lang.IndexOutOfBoundsException
- if the index is out of rangejava.lang.ClassCastException
- if the value at the specified position is not
JsonValue.TRUE
or JsonValue.FALSE
boolean getBoolean(int index, boolean defaultValue)
JsonValue.TRUE
this method returns true
. If the value at the specified position
is JsonValue.FALSE
this method returns false
.
Otherwise this method returns the specified default value.index
- index of the JSON boolean valuedefaultValue
- the boolean value to return if the JsonValue
at the specified position is neither TRUE nor FALSEboolean isNull(int index)
true
if the value at the specified location in this
array is JsonValue.NULL
.index
- index of the JSON null valueJsonValue.NULL
, otherwise falsejava.lang.IndexOutOfBoundsException
- if the index is out of range