javax.json
Interface JsonArray

All Superinterfaces:
JsonStructure, JsonValue

public interface JsonArray
extends JsonStructure

JsonArray class represents an immutable JSON array value.

A full 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 JsonBuilder.beginArray().

For example 1:

 An empty JSON array can be built as follows:

 JsonArray array = new JsonBuilder()
     .beginArray()
     .endArray()
 .build();
 

For example 2:

 The following JSON

 [
     { "type": "home", "number": "212 555-1234" },
     { "type": "fax", "number": "646 555-4567" }
 ]

 can be built using :

 JsonArray array = new JsonBuilder()
     .beginArray()
         .beginObject()
             .add("type", "home")
             .add("number", "212 555-1234")
         .endObject()
         .beginObject()
             .add("type", "home")
             .add("number", "646 555-4567")
         .endObject()
     .endArray()
 .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. For example:

 In the above example 2, home number "212 555-1234" can be got using:

 JsonObject home = array.getValue(0, JsonObject.class);
 String number = home.getValue("number", JsonString.class).getValue();
 

Author:
Jitendra Kotamraju

Nested Class Summary
 
Nested classes/interfaces inherited from interface javax.json.JsonValue
JsonValue.JsonValueType
 
Field Summary
 
Fields inherited from interface javax.json.JsonValue
FALSE, NULL, TRUE
 
Method Summary
 JsonValue getValue(int index)
          Returns the value at the specified position in this JSON array values.
<T extends JsonValue>
T
getValue(int index, Class<T> clazz)
          Returns the value at the specified position in this JSON array values.
 List<JsonValue> getValues()
          Returns an unmodifiable list of this JSON array values
 int size()
          Returns the number of values in this JSON array.
 
Methods inherited from interface javax.json.JsonValue
getValueType
 

Method Detail

getValues

List<JsonValue> getValues()
Returns an unmodifiable list of this JSON array values

Returns:
a list of array values

size

int size()
Returns the number of values in this JSON array. If this array contains more than Integer.MAX_VALUE values, returns Integer.MAX_VALUE.

Returns:
the number of values in this JSON array

getValue

JsonValue getValue(int index)
Returns the value at the specified position in this JSON array values.

Parameters:
index - index of the value to return
Returns:
the value at the specified position in this array values
Throws:
IndexOutOfBoundsException - if the index is out of range

getValue

<T extends JsonValue> T getValue(int index,
                                 Class<T> clazz)
Returns the value at the specified position in this JSON array values.

Parameters:
index - index of the value to return
clazz - value class
Returns:
the value at the specified position in this array values
Throws:
IndexOutOfBoundsException - if the index is out of range
ClassCastException - if the value at the specified position is not assignable to the type T



Copyright © 2012 Oracle and/or its affiliates. All rights reserved.