|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface JsonObject
JsonObject
class represents an immutable JSON object value.
A full JsonObject instance can be created from an input source using
JsonReader.readObject()
. For example:
It can also be built from scratch using
JsonReader jsonReader = new JsonReader(...));
JsonObject object = jsonReader.readObject();
jsonReader.close();
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();
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. |
|
|
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 |
---|
JsonValue getValue(String name)
null
if this object contains no mapping for the name/key.
name
- the name/key whose associated value is to be returned
null
if this object contains no mapping for the name/key<T extends JsonValue> T getValue(String name, Class<T> clazz)
null
if this object contains no mapping for the name/key.
name
- the name/key whose associated value is to be returnedclazz
- value class
null
if this object contains no mapping for the name/key
ClassCastException
- if the value for specified name/key mapping
is not assignable to the type TSet<String> getNames()
Set
of the name/keys contained in this
JSON object.
Map<String,JsonValue> getValues()
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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |