public interface JsonArray extends JsonStructure, List<JsonValue>
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
JsonReader.readArray(). For example:
JsonReader jsonReader = new JsonReader(...);
JsonArray array = jsonReader.readArray();
jsonReader.close();
It can also be built from scratch using a 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();
JsonArray can be written to JSON as follows:
JsonArray arr = ...;
JsonWriter writer = new JsonWriter(...)
writer.writeArray(arr);
writer.close();
JsonArray values can be JsonObject, JsonArray,
JsonString, JsonNumber, JsonValue.TRUE,
JsonValue.FALSE, 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 UnsupportedOperationException.JsonValue.ValueType| Modifier and Type | Method and Description |
|---|---|
int |
getIntValue(int index)
A Convenience method for
getValue(index, JsonNumber.class).getIntValue() |
String |
getStringValue(int index)
A convenience method for
getValue(index, JsonString.class).getValue() |
<T extends JsonValue> |
getValue(int index,
Class<T> clazz)
Returns the value at the specified position in this JSON array values.
|
getValueType, toString<T extends JsonValue> T getValue(int index, Class<T> clazz)
(T) get(index) to get
the value.index - index of the value to returnclazz - value classIndexOutOfBoundsException - if the index is out of rangeClassCastException - if the value at the specified position is not
assignable to the type TString getStringValue(int index)
getValue(index, JsonString.class).getValue()index - index of the JsonString valueIndexOutOfBoundsException - if the index is out of rangeClassCastException - if the value at the specified position is not
assignable to JsonStringint getIntValue(int index)
getValue(index, JsonNumber.class).getIntValue()index - index of the JsonNumber valueIndexOutOfBoundsException - if the index is out of rangeClassCastException - if the value at the specified position is not
assignable to JsonNumberCopyright © 2013. All Rights Reserved.