Interface JsonArray
- All Superinterfaces:
Collection<JsonValue>,Iterable<JsonValue>,JsonStructure,JsonValue,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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface javax.json.JsonValue
JsonValue.ValueType -
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionbooleangetBoolean(int index) Returns the boolean value at the specified position.booleangetBoolean(int index, boolean defaultValue) Returns the boolean value at the specified position.intgetInt(int index) A convenience method forgetJsonNumber(index).intValue().intgetInt(int index, int defaultValue) Returns the int value of theJsonNumberat the specified position.getJsonArray(int index) Returns the array value at the specified position in this array.getJsonNumber(int index) Returns the number value at the specified position in this array.getJsonObject(int index) Returns the object value at the specified position in this array.getJsonString(int index) Returns the string value at ths specified position in this array.getString(int index) A convenience method forgetJsonString(index).getString().Returns theStringvalue ofJsonStringat the specified position in this JSON array values.getValuesAs(Class<T> clazz) Returns a list a view of the specified type for the array.booleanisNull(int index) Returnstrueif the value at the specified location in this array isJsonValue.NULL.Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface javax.json.JsonValue
getValueType, toStringMethods inherited from interface java.util.List
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
-
Method Details
-
getJsonObject
Returns the object value at the specified position in this array. This is a convenience method for(JsonObject)get(index).- Parameters:
index- index of the value to be returned- Returns:
- the value at the specified position in this array
- Throws:
IndexOutOfBoundsException- if the index is out of rangeClassCastException- if the value at the specified position is not assignable to the JsonObject type
-
getJsonArray
Returns the array value at the specified position in this array. This is a convenience method for(JsonArray)get(index).- Parameters:
index- index of the value to be returned- Returns:
- the value at the specified position in this array
- Throws:
IndexOutOfBoundsException- if the index is out of rangeClassCastException- if the value at the specified position is not assignable to the JsonArray type
-
getJsonNumber
Returns the number value at the specified position in this array. This is a convenience method for(JsonNumber)get(index).- Parameters:
index- index of the value to be returned- Returns:
- the value at the specified position in this array
- Throws:
IndexOutOfBoundsException- if the index is out of rangeClassCastException- if the value at the specified position is not assignable to the JsonNumber type
-
getJsonString
Returns the string value at ths specified position in this array. This is a convenience method for(JsonString)get(index).- Parameters:
index- index of the value to be returned- Returns:
- the value at the specified position in this array
- Throws:
IndexOutOfBoundsException- if the index is out of rangeClassCastException- if the value at the specified position is not assignable to the JsonString type
-
getValuesAs
Returns a list a view of the specified type for the array. This method does not verify if there is a value of wrong type in the array. Providing this typesafe view dynamically may cause a program fail with aClassCastException, if there is a value of wrong type in this array. Unfortunately, the exception can occur at any time after this method returns.- Parameters:
clazz- a JsonValue type- Returns:
- a list view of the specified type
-
getString
A convenience method forgetJsonString(index).getString().- Parameters:
index- index of theJsonStringvalue- Returns:
- the String value at the specified position
- Throws:
IndexOutOfBoundsException- if the index is out of rangeClassCastException- if the value at the specified position is not assignable toJsonString
-
getString
Returns theStringvalue ofJsonStringat the specified position in this JSON array values. IfJsonStringis found, itsJsonString.getString()is returned. Otherwise, the specified default value is returned.- Parameters:
index- index of the JsonString value- Returns:
- the String value at the specified position in this array, or the specified default value
-
getInt
int getInt(int index) A convenience method forgetJsonNumber(index).intValue().- Parameters:
index- index of theJsonNumbervalue- Returns:
- the int value at the specified position
- Throws:
IndexOutOfBoundsException- if the index is out of rangeClassCastException- if the value at the specified position is not assignable toJsonNumber
-
getInt
int getInt(int index, int defaultValue) Returns the int value of theJsonNumberat the specified position. If the value at that position is aJsonNumber, this method returnsJsonNumber.intValue(). Otherwise this method returns the specified default value.- Parameters:
index- index of theJsonNumbervalue- Returns:
- the int value at the specified position in this array, or the specified default value
-
getBoolean
boolean getBoolean(int index) Returns the boolean value at the specified position. If the value at the specified position isJsonValue.TRUEthis method returnstrue. If the value at the specified position isJsonValue.FALSEthis method returnsfalse.- Parameters:
index- index of the JSON boolean value- Returns:
- the boolean value at the specified position
- Throws:
IndexOutOfBoundsException- if the index is out of rangeClassCastException- if the value at the specified position is not assignable toJsonValue.TRUEorJsonValue.FALSE
-
getBoolean
boolean getBoolean(int index, boolean defaultValue) Returns the boolean value at the specified position. If the value at the specified position isJsonValue.TRUEthis method returnstrue. If the value at the specified position isJsonValue.FALSEthis method returnsfalse. Otherwise this method returns the specified default value.- Parameters:
index- index of the JSON boolean value- Returns:
- the boolean value at the specified position, or the specified default value
-
isNull
boolean isNull(int index) Returnstrueif the value at the specified location in this array isJsonValue.NULL.- Parameters:
index- index of the JSON null value- Returns:
- return true if the value at the specified location is
JsonValue.NUL, otherwise false - Throws:
IndexOutOfBoundsException- if the index is out of range
-