-
- All Superinterfaces:
- JsonStructure, JsonValue, Map<String,JsonValue>
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:
It can also be built from scratch using aJsonReader jsonReader = Json.createReader(...); JsonObject object = jsonReader.readObject(); jsonReader.close();
JsonObjectBuilder
.For example 1: An empty JSON object can be built as follows:
For example 2: The following JSONJsonObject object = Json.createObjectBuilder().build();
can be built using :{ "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 beJsonObject
,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
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 anString 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.
- Author:
- Jitendra Kotamraju
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface javax.json.JsonValue
JsonValue.ValueType
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method and Description boolean
getBoolean(String name)
Returns the boolean value of the associated mapping for the specified name.boolean
getBoolean(String name, boolean defaultValue)
Returns the boolean value of the associated mapping for the specified name.int
getInt(String name)
A convenience method forgetJsonNumber(name).intValue()
int
getInt(String name, int defaultValue)
Returns the int value of the associatedJsonNumber
mapping for the specified name.JsonArray
getJsonArray(String name)
Returns the array value to which the specified name is mapped.JsonNumber
getJsonNumber(String name)
Returns the number value to which the specified name is mapped.JsonObject
getJsonObject(String name)
Returns the object value to which the specified name is mapped.JsonString
getJsonString(String name)
Returns the string value to which the specified name is mapped.String
getString(String name)
A convenience method forgetJsonString(name).getString()
String
getString(String name, String defaultValue)
Returns the string value of the associatedJsonString
mapping for the specified name.boolean
isNull(String name)
Returnstrue
if the associated value for the specified name isJsonValue.NULL
.-
Methods inherited from interface javax.json.JsonValue
getValueType, toString
-
Methods inherited from interface java.util.Map
clear, 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 Detail
-
getJsonArray
JsonArray getJsonArray(String name)
Returns 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
null
if 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
-
getJsonObject
JsonObject getJsonObject(String name)
Returns 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
null
if 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
-
getJsonNumber
JsonNumber getJsonNumber(String name)
Returns 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
null
if 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
-
getJsonString
JsonString getJsonString(String name)
Returns 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
null
if 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
-
getString
String getString(String name)
A 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 mappingClassCastException
- if the value for specified name mapping is not assignable to JsonString
-
getString
String getString(String name, String defaultValue)
Returns the string value of the associatedJsonString
mapping for the specified name. IfJsonString
is found, then itsJsonString.getString()
is returned. Otherwise, the specified default value is returned.- Parameters:
name
- whose associated value is to be returned as StringdefaultValue
- a default value to be returned- Returns:
- the string value of the associated mapping for the name, or the default value
-
getInt
int getInt(String name)
A 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 mappingClassCastException
- if the value for specified name mapping is not assignable to JsonNumber
-
getInt
int getInt(String name, int defaultValue)
Returns the int value of the associatedJsonNumber
mapping for the specified name. IfJsonNumber
is found, then itsJsonNumber.intValue()
is returned. Otherwise, the specified default value is returned.- Parameters:
name
- whose associated value is to be returned as intdefaultValue
- a default value to be returned- Returns:
- the int value of the associated mapping for the name, or the default value
-
getBoolean
boolean getBoolean(String name)
Returns 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 mappingClassCastException
- if the value for specified name mapping is not assignable to JsonValue.TRUE or JsonValue.FALSE
-
getBoolean
boolean getBoolean(String name, boolean defaultValue)
Returns 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 intdefaultValue
- a default value to be returned- Returns:
- the boolean value of the associated mapping for the name, or the default value
-
isNull
boolean isNull(String name)
Returnstrue
if 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.NUL
, otherwise false - Throws:
NullPointerException
- if the specified name doesn't have any mapping
-
-
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 11/06/2005 gemaakt, de laatste keer de 18/08/2025 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-javaee-rf-javax/json/jsonobject.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur van deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.