public interface JsonObject extends JsonStructure, Map<String,JsonValue>
JsonObject
class represents an immutable JSON object value
(an unordered collection of zero or more name/value pairs).
It also provides unmodifiable map view to the JSON object
name/value mappings.
A 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 a JsonObjectBuilder
.
For example 1: An empty JSON object can be built as follows:
JsonObject object = new JsonObjectBuilder().build();
For example 2: The following JSON
{
"firstName": "John", "lastName": "Smith", "age": 25,
"address" : {
"streetAddress", "21 2nd Street",
"city", "New York",
"state", "NY",
"postalCode", "10021"
},
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
can be built using :
JsonObject value = new JsonObjectBuilder()
.add("firstName", "John")
.add("lastName", "Smith")
.add("age", 25)
.add("address", new JsonObjectBuilder()
.add("streetAddress", "21 2nd Street")
.add("city", "New York")
.add("state", "NY")
.add("postalCode", "10021"))
.add("phoneNumber", 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();
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.
In the above example 2, "John" can be got using
String firstName = object.getStringValue("firstName");
This map object provides read-only access to the JSON object data,
and attempts to modify the map, whether direct or via its collection
views, result in an UnsupportedOperationException
.
The map object's iteration ordering is based on the order in which name/value pairs are added to the corresponding builder or the order in which name/value pairs appear in the corresponding stream.
JsonValue.ValueType
Modifier and Type | Method and Description |
---|---|
int |
getIntValue(String name)
A convenience method for
getValue(name, JsonNumber.class).getIntValue() |
String |
getStringValue(String name)
A convenience method for
getValue(name, JsonString.class).getValue() |
<T extends JsonValue> |
getValue(String name,
Class<T> clazz)
Returns the value to which the specified name is mapped.
|
getValueType, toString
<T extends JsonValue> T getValue(String name, Class<T> clazz)
(T) get(name)
to get
the value.name
- the name(key) whose associated value is to be returnedclazz
- value classnull
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 TString getStringValue(String name)
getValue(name, JsonString.class).getValue()
name
- whose associated value is to be returned as Stringnull
if this object contains no mapping for the nameClassCastException
- if the value for specified name mapping
is not assignable to JsonStringint getIntValue(String name)
getValue(name, JsonNumber.class).getIntValue()
name
- whose associated value is to be returned as intnull
if this object contains no mapping for the nameClassCastException
- if the value for specified name mapping
is not assignable to JsonNumberCopyright © 2013. All Rights Reserved.