Module jakarta.json
Package jakarta.json

Interface JsonArray

All Superinterfaces:
Collection<JsonValue>, Iterable<JsonValue>, JsonStructure, JsonValue, List<JsonValue>, SequencedCollection<JsonValue>

public interface JsonArray extends JsonStructure, 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.