javax.json
Interface JsonObject

All Superinterfaces:
JsonStructure, JsonValue

public interface JsonObject
extends JsonStructure

JsonObject class represents an immutable JSON object value.

A full JsonObject instance can be created from an input source using JsonReader.readObject(). For example:

 JsonReader jsonReader = new JsonReader(...));
 JsonObject object = jsonReader.readObject();
 jsonReader.close();
 
It can also be built from scratch using JsonBuilder.beginObject().

For example 1:

 An empty JSON object can be built as follows:

 JsonArray array = new JsonBuilder()
     .beginObject()
     .endObject()
 .build();
 

For example 2:

 The following JSON

 {
     "firstName": "John", "lastName": "Smith", "age": 25,
     "phoneNumber": [
         {"type": "home", "number": "212 555-1234"},
         {"type": "fax", "number": "646 555-4567"}
      ]
 }

 can be built using :

 JsonObject object = new JsonBuilder()
     .beginObject()
         .add("firstName", "John")
         .add("lastName", "Smith")
         .add("age", 25)
         .beginObject("address")
             .add("streetAddress", "21 2nd Street")
             .add("city", "New York")
             .add("state", "NY")
             .add("postalCode", "10021")
         .endObject()
         .beginArray("phoneNumber")
             .beginObject()
                 .add("type", "home")
                 .add("number", "212 555-1234")
             .endObject()
             .beginObject()
                 .add("type", "home")
                 .add("number", "646 555-4567")
             .endObject()
         .endArray()
     .endObject()
 .build();
 
JsonObject can be written to JSON as follows:
 JsonWriter writer = ...
 JsonObject obj = ...;
 writer.writeobject(obj);
 

JsonObject 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, "John" can be got using

 String firstName = object.getValue("firstName", 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
 Set<String> getNames()
          Returns an unmodifiable Set of the name/keys contained in this JSON object.
 JsonValue getValue(String name)
          Returns the value to which the specified name/key is mapped, or null if this object contains no mapping for the name/key.
<T extends JsonValue>
T
getValue(String name, Class<T> clazz)
          Returns the value to which the specified name/key is mapped, or null if this object contains no mapping for the name/key.
 Map<String,JsonValue> getValues()
          Returns an unmodifiable Map of the name(key)/value pairs contained in this JSON object.
 
Methods inherited from interface javax.json.JsonValue
getValueType
 

Method Detail

getValue

JsonValue getValue(String name)
Returns the value to which the specified name/key is mapped, or null if this object contains no mapping for the name/key.

Parameters:
name - the name/key whose associated value is to be returned
Returns:
the value to which the specified name is mapped, or null if this object contains no mapping for the name/key

getValue

<T extends JsonValue> T getValue(String name,
                                 Class<T> clazz)
Returns the value to which the specified name/key is mapped, or null if this object contains no mapping for the name/key.

Parameters:
name - the name/key whose associated value is to be returned
clazz - value class
Returns:
the value to which the specified name is mapped, or null if this object contains no mapping for the name/key
Throws:
ClassCastException - if the value for specified name/key mapping is not assignable to the type T

getNames

Set<String> getNames()
Returns an unmodifiable Set of the name/keys contained in this JSON object.

Returns:
a set of the name/keys contained in this JSON object

getValues

Map<String,JsonValue> getValues()
Returns an unmodifiable Map of the name(key)/value pairs contained in this JSON object. The iteration order of the map is predictable which is normally the order in which name/value pairs were added into this JSON Object.

Returns:
a set of the name/keys contained in this JSON object



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