{@code JsonArray} class represents an immutable JSON array value(an ordered sequence of zero or more values). It also provides unmodifiable list view to the JSON array values.
A JsonArray instance can be created from a input source using {@link JsonReader#readArray()}. For example:
JsonReader jsonReader = new JsonReader(...); JsonArray array = jsonReader.readArray(); jsonReader.close();
It can also be built from scratch using a {@link JsonArrayBuilder}.
For example 1: An empty JSON array can be built as follows:
JsonArray array = new JsonArrayBuilder().build();
For example 2: The following JSON
[ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ]
can be built using :
JsonArray value = new JsonArrayBuilder() .add(new JsonObjectBuilder() .add("type", "home") .add("number", "212 555-1234")) .add(new JsonObjectBuilder() .add("type", "fax") .add("number", "646 555-4567")) .build();
{@code JsonArray} can be written to JSON as follows:
JsonArray arr = ...; JsonWriter writer = new JsonWriter(...) writer.writeArray(arr); writer.close();
{@code JsonArray} values can be {@link JsonObject}, {@link JsonArray}, {@link JsonString}, {@link JsonNumber}, {@link JsonValue#TRUE}, {@link JsonValue#FALSE}, {@link JsonValue#NULL}. These values can be accessed using various accessor methods.
In the above example 2, home number "212 555-1234" can be got using:
JsonObject home = array.getValue(0, JsonObject.class); String number = home.getStringValue("number");
This list object provides read-only access to the JSON array data, and attempts to modify the list, whether direct or via its collection views, result in an {@code UnsupportedOperationException}.
@author Jitendra Kotamraju