Package jakarta.json
Interface JsonObject
- All Superinterfaces:
- JsonStructure,- JsonValue,- 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 = Json.createReader(...);
 JsonObject object = jsonReader.readObject();
 jsonReader.close();
 JsonObjectBuilder.
 For example 1: An empty JSON object can be built as follows:
 JsonObject object = Json.createObjectBuilder().build();
 
 {
     "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" }
     ]
 }
 
 JsonObject value = Json.createObjectBuilder()
     .add("firstName", "John")
     .add("lastName", "Smith")
     .add("age", 25)
     .add("address", Json.createObjectBuilder()
         .add("streetAddress", "21 2nd Street")
         .add("city", "New York")
         .add("state", "NY")
         .add("postalCode", "10021"))
     .add("phoneNumber", Json.createArrayBuilder()
         .add(Json.createObjectBuilder()
             .add("type", "home")
             .add("number", "212 555-1234"))
         .add(Json.createObjectBuilder()
             .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.getString("firstName");
 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.
- 
Nested Class SummaryNested classes/interfaces inherited from interface jakarta.json.JsonValueJsonValue.ValueType
- 
Field SummaryFields inherited from interface jakarta.json.JsonValueEMPTY_JSON_ARRAY, EMPTY_JSON_OBJECT, FALSE, NULL, TRUE
- 
Method SummaryModifier and TypeMethodDescriptionbooleangetBoolean(String name) Returns the boolean value of the associated mapping for the specified name.booleangetBoolean(String name, boolean defaultValue) Returns the boolean value of the associated mapping for the specified name.intA convenience method forgetJsonNumber(name).intValue()intReturns the int value of the associatedJsonNumbermapping for the specified name.getJsonArray(String name) Returns the array value to which the specified name is mapped.getJsonNumber(String name) Returns the number value to which the specified name is mapped.getJsonObject(String name) Returns the object value to which the specified name is mapped.getJsonString(String name) Returns the string value to which the specified name is mapped.A convenience method forgetJsonString(name).getString()Returns the string value of the associatedJsonStringmapping for the specified name.booleanReturnstrueif the associated value for the specified name isJsonValue.NULL.Methods inherited from interface jakarta.json.JsonStructuregetValueMethods inherited from interface jakarta.json.JsonValueasJsonArray, asJsonObject, getValueType, toStringMethods inherited from interface java.util.Mapclear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
- 
Method Details- 
getJsonArrayReturns the array value to which the specified name is mapped. This is a convenience method for(JsonArray)get(name)to get the value.- Parameters:
- name- the name whose associated value is to be returned
- Returns:
- the array value to which the specified name is mapped, or
         nullif this object contains no mapping for the name
- Throws:
- ClassCastException- if the value to which the specified name is mapped is not assignable to JsonArray type
 
- 
getJsonObjectReturns the object value to which the specified name is mapped. This is a convenience method for(JsonObject)get(name)to get the value.- Parameters:
- name- the name whose associated value is to be returned
- Returns:
- the object value to which the specified name is mapped, or
         nullif this object contains no mapping for the name
- Throws:
- ClassCastException- if the value to which the specified name is mapped is not assignable to JsonObject type
 
- 
getJsonNumberReturns the number value to which the specified name is mapped. This is a convenience method for(JsonNumber)get(name)to get the value.- Parameters:
- name- the name whose associated value is to be returned
- Returns:
- the number value to which the specified name is mapped, or
         nullif this object contains no mapping for the name
- Throws:
- ClassCastException- if the value to which the specified name is mapped is not assignable to JsonNumber type
 
- 
getJsonStringReturns the string value to which the specified name is mapped. This is a convenience method for(JsonString)get(name)to get the value.- Parameters:
- name- the name whose associated value is to be returned
- Returns:
- the string value to which the specified name is mapped, or
         nullif this object contains no mapping for the name
- Throws:
- ClassCastException- if the value to which the specified name is mapped is not assignable to JsonString type
 
- 
getStringA convenience method forgetJsonString(name).getString()- Parameters:
- name- whose associated value is to be returned as String
- Returns:
- the String value to which the specified name is mapped
- Throws:
- NullPointerException- if the specified name doesn't have any mapping
- ClassCastException- if the value for specified name mapping is not assignable to JsonString
 
- 
getStringReturns the string value of the associatedJsonStringmapping for the specified name. IfJsonStringis found, then itsJsonString.getString()is returned. Otherwise, the specified default value is returned.- Parameters:
- name- whose associated value is to be returned as String
- defaultValue- a default value to be returned
- Returns:
- the string value of the associated mapping for the name, or the default value
 
- 
getIntA convenience method forgetJsonNumber(name).intValue()- Parameters:
- name- whose associated value is to be returned as int
- Returns:
- the int value to which the specified name is mapped
- Throws:
- NullPointerException- if the specified name doesn't have any mapping
- ClassCastException- if the value for specified name mapping is not assignable to JsonNumber
 
- 
getIntReturns the int value of the associatedJsonNumbermapping for the specified name. IfJsonNumberis found, then itsJsonNumber.intValue()is returned. Otherwise, the specified default value is returned.- Parameters:
- name- whose associated value is to be returned as int
- defaultValue- a default value to be returned
- Returns:
- the int value of the associated mapping for the name, or the default value
 
- 
getBooleanReturns the boolean value of the associated mapping for the specified name. If the associated mapping is JsonValue.TRUE, then returns true. If the associated mapping is JsonValue.FALSE, then returns false.- Parameters:
- name- whose associated value is to be returned as boolean
- Returns:
- the boolean value to which the specified name is mapped
- Throws:
- NullPointerException- if the specified name doesn't have any mapping
- ClassCastException- if the value for specified name mapping is not assignable to JsonValue.TRUE or JsonValue.FALSE
 
- 
getBooleanReturns the boolean value of the associated mapping for the specified name. If the associated mapping is JsonValue.TRUE, then returns true. If the associated mapping is JsonValue.FALSE, then returns false. Otherwise, the specified default value is returned.- Parameters:
- name- whose associated value is to be returned as int
- defaultValue- a default value to be returned
- Returns:
- the boolean value of the associated mapping for the name, or the default value
 
- 
isNullReturnstrueif the associated value for the specified name isJsonValue.NULL.- Parameters:
- name- name whose associated value is checked
- Returns:
- return true if the associated value is JsonValue.NULL, otherwise false
- Throws:
- NullPointerException- if the specified name doesn't have any mapping
 
 
-