public class JSONObject extends JSONValue implements Iterable<JSONObject.Member>
Members can be added using the add(String, ...) methods which
accept instances of JSONValue, strings, primitive numbers, and
boolean values. To modify certain values of an object, use the
set(String, ...) methods. Please note that the add
methods are faster than set as they do not search for existing
members. On the other hand, the add methods do not prevent
adding multiple members with the same name. Duplicate names are discouraged
but not prohibited by JSON.
Members can be accessed by their name using get(String). A list of
all names can be obtained from the method names(). This class also
supports iterating over the members in document order using an
iterator() or an enhanced for loop:
for (Member member : jsonObject) {
String name = member.getName();
JsonValue value = member.getValue();
...
}
Even though JSON objects are unordered by definition, instances of this class preserve the order of members to allow processing in document order and to guarantee a predictable output.
Note that this class is not thread-safe. If multiple threads
access a JsonObject instance concurrently, while at least one of
these threads modifies the contents of this object, access to the instance
must be synchronized externally. Failure to do so may lead to an inconsistent
state.
This class is not supposed to be extended by clients.
| 限定符和类型 | 类和说明 |
|---|---|
static class |
JSONObject.Member
Represents a member of a JSON object, a pair of a name and a value.
|
| 构造器和说明 |
|---|
JSONObject()
Creates a new empty JsonObject.
|
JSONObject(JSONObject object)
Creates a new JsonObject, initialized with the contents of the specified
JSON object.
|
| 限定符和类型 | 方法和说明 |
|---|---|
JSONObject |
asJSONObject()
Returns this JSON value as
JSONObject, assuming that this value
represents a JSON object. |
boolean |
contains(String name) |
boolean |
equals(Object obj)
Indicates whether some other object is "equal to" this one according to
the contract specified in
Object.equals(Object). |
JSONValue |
get(String name)
Returns the value of the member with the specified name in this object.
|
boolean |
getBoolean(String name) |
Boolean |
getBoolean(String name,
Boolean defaultValue)
Returns the
boolean value of the member with the specified
name in this object. |
Double |
getDouble(String name) |
Double |
getDouble(String name,
Double defaultValue)
Returns the
double value of the member with the specified
name in this object. |
Float |
getFloat(String name) |
Float |
getFloat(String name,
Float defaultValue)
Returns the
float value of the member with the specified
name in this object. |
Integer |
getInt(String name) |
Integer |
getInt(String name,
Integer defaultValue)
Returns the
int value of the member with the specified name
in this object. |
Long |
getLong(String name) |
Long |
getLong(String name,
Long defaultValue)
Returns the
long value of the member with the specified name
in this object. |
String |
getString(String name) |
String |
getString(String name,
String defaultValue)
Returns the
String value of the member with the specified
name in this object. |
int |
hashCode() |
boolean |
isEmpty()
Returns
true if this object contains no members. |
boolean |
isObject()
Detects whether this value represents a JSON object.
|
Iterator<JSONObject.Member> |
iterator()
Returns an iterator over the members of this object in document order.
|
JSONObject |
merge(JSONObject object)
Copies all members of the specified object into this object.
|
List<String> |
names()
Returns a list of the names in this object in document order.
|
JSONObject |
put(String name,
boolean value)
Appends a new member to the end of this object, with the specified name
and the JSON representation of the specified
boolean value. |
JSONObject |
put(String name,
double value)
Appends a new member to the end of this object, with the specified name
and the JSON representation of the specified
double value. |
JSONObject |
put(String name,
float value)
Appends a new member to the end of this object, with the specified name
and the JSON representation of the specified
float value. |
JSONObject |
put(String name,
int value)
Appends a new member to the end of this object, with the specified name
and the JSON representation of the specified
int value. |
JSONObject |
put(String name,
JSONValue value)
Appends a new member to the end of this object, with the specified name
and the specified JSON value.
|
JSONObject |
put(String name,
long value)
Appends a new member to the end of this object, with the specified name
and the JSON representation of the specified
long value. |
JSONObject |
put(String name,
Object value) |
JSONObject |
put(String name,
String value)
Appends a new member to the end of this object, with the specified name
and the JSON representation of the specified string.
|
JSONObject |
remove(String name)
Removes a member with the specified name from this object.
|
JSONObject |
set(String name,
boolean value)
Sets the value of the member with the specified name to the JSON
representation of the specified
boolean value. |
JSONObject |
set(String name,
double value)
Sets the value of the member with the specified name to the JSON
representation of the specified
double value. |
JSONObject |
set(String name,
float value)
Sets the value of the member with the specified name to the JSON
representation of the specified
float value. |
JSONObject |
set(String name,
int value)
Sets the value of the member with the specified name to the JSON
representation of the specified
int value. |
JSONObject |
set(String name,
JSONValue value)
Sets the value of the member with the specified name to the specified
JSON value.
|
JSONObject |
set(String name,
long value)
Sets the value of the member with the specified name to the JSON
representation of the specified
long value. |
JSONObject |
set(String name,
String value)
Sets the value of the member with the specified name to the JSON
representation of the specified string.
|
int |
size()
Returns the number of members (name/value pairs) in this object.
|
static JSONObject |
unmodifiableObject(JSONObject object)
Returns an unmodifiable JsonObject for the specified one.
|
asArray, asBoolean, asDouble, asFloat, asInt, asLong, asString, isArray, isBean, isBoolean, isFalse, isLong, isNull, isNumber, isString, isTrue, toString, toString, writeTo, writeToforEach, spliteratorpublic JSONObject()
public JSONObject(JSONObject object)
object - the JSON object to get the initial contents from, must not be
nullpublic static JSONObject unmodifiableObject(JSONObject object)
The returned JsonObject is backed by the given object and reflect changes
that happen to it. Attempts to modify the returned JsonObject result in
an UnsupportedOperationException.
object - the JsonObject for which an unmodifiable JsonObject is to be
returnedpublic JSONObject put(String name, int value)
int value.
This method does not prevent duplicate names. Calling
this method with a name that already exists in the object will append
another member with the same name. In order to replace existing members,
use the method set(name, value) instead. However,
add is much faster than set (because it does
not need to search for existing members). Therefore add should
be preferred when constructing new objects.
name - the name of the member to addvalue - the value of the member to addpublic JSONObject put(String name, Object value)
public JSONObject put(String name, long value)
long value.
This method does not prevent duplicate names. Calling
this method with a name that already exists in the object will append
another member with the same name. In order to replace existing members,
use the method set(name, value) instead. However,
add is much faster than set (because it does
not need to search for existing members). Therefore add should
be preferred when constructing new objects.
name - the name of the member to addvalue - the value of the member to addpublic JSONObject put(String name, float value)
float value.
This method does not prevent duplicate names. Calling
this method with a name that already exists in the object will append
another member with the same name. In order to replace existing members,
use the method set(name, value) instead. However,
add is much faster than set (because it does
not need to search for existing members). Therefore add should
be preferred when constructing new objects.
name - the name of the member to addvalue - the value of the member to addpublic JSONObject put(String name, double value)
double value.
This method does not prevent duplicate names. Calling
this method with a name that already exists in the object will append
another member with the same name. In order to replace existing members,
use the method set(name, value) instead. However,
add is much faster than set (because it does
not need to search for existing members). Therefore add should
be preferred when constructing new objects.
name - the name of the member to addvalue - the value of the member to addpublic JSONObject put(String name, boolean value)
boolean value.
This method does not prevent duplicate names. Calling
this method with a name that already exists in the object will append
another member with the same name. In order to replace existing members,
use the method set(name, value) instead. However,
add is much faster than set (because it does
not need to search for existing members). Therefore add should
be preferred when constructing new objects.
name - the name of the member to addvalue - the value of the member to addpublic JSONObject put(String name, String value)
This method does not prevent duplicate names. Calling
this method with a name that already exists in the object will append
another member with the same name. In order to replace existing members,
use the method set(name, value) instead. However,
add is much faster than set (because it does
not need to search for existing members). Therefore add should
be preferred when constructing new objects.
name - the name of the member to addvalue - the value of the member to addpublic JSONObject put(String name, JSONValue value)
This method does not prevent duplicate names. Calling
this method with a name that already exists in the object will append
another member with the same name. In order to replace existing members,
use the method set(name, value) instead. However,
add is much faster than set (because it does
not need to search for existing members). Therefore add should
be preferred when constructing new objects.
name - the name of the member to addvalue - the value of the member to add, must not be nullpublic JSONObject set(String name, int value)
int value. If this object
does not contain a member with this name, a new member is added at the
end of the object. If this object contains multiple members with this
name, only the last one is changed.
This method should only be used to modify existing
objects. To fill a new object with members, the method
add(name, value) should be preferred which is much faster
(as it does not need to search for existing members).
name - the name of the member to replacevalue - the value to set to the memberpublic JSONObject set(String name, long value)
long value. If this object
does not contain a member with this name, a new member is added at the
end of the object. If this object contains multiple members with this
name, only the last one is changed.
This method should only be used to modify existing
objects. To fill a new object with members, the method
add(name, value) should be preferred which is much faster
(as it does not need to search for existing members).
name - the name of the member to replacevalue - the value to set to the memberpublic JSONObject set(String name, float value)
float value. If this object
does not contain a member with this name, a new member is added at the
end of the object. If this object contains multiple members with this
name, only the last one is changed.
This method should only be used to modify existing
objects. To fill a new object with members, the method
add(name, value) should be preferred which is much faster
(as it does not need to search for existing members).
name - the name of the member to addvalue - the value of the member to addpublic JSONObject set(String name, double value)
double value. If this object
does not contain a member with this name, a new member is added at the
end of the object. If this object contains multiple members with this
name, only the last one is changed.
This method should only be used to modify existing
objects. To fill a new object with members, the method
add(name, value) should be preferred which is much faster
(as it does not need to search for existing members).
name - the name of the member to addvalue - the value of the member to addpublic JSONObject set(String name, boolean value)
boolean value. If this
object does not contain a member with this name, a new member is added at
the end of the object. If this object contains multiple members with this
name, only the last one is changed.
This method should only be used to modify existing
objects. To fill a new object with members, the method
add(name, value) should be preferred which is much faster
(as it does not need to search for existing members).
name - the name of the member to addvalue - the value of the member to addpublic JSONObject set(String name, String value)
This method should only be used to modify existing
objects. To fill a new object with members, the method
add(name, value) should be preferred which is much faster
(as it does not need to search for existing members).
name - the name of the member to addvalue - the value of the member to addpublic JSONObject set(String name, JSONValue value)
This method should only be used to modify existing
objects. To fill a new object with members, the method
add(name, value) should be preferred which is much faster
(as it does not need to search for existing members).
name - the name of the member to addvalue - the value of the member to add, must not be nullpublic JSONObject remove(String name)
name - the name of the member to removepublic JSONObject merge(JSONObject object)
object - the object to mergepublic JSONValue get(String name)
name - the name of the member whose value is to be returnednull if this object does not contain a member with
that namepublic boolean contains(String name)
public Integer getInt(String name, Integer defaultValue)
int value of the member with the specified name
in this object. If this object does not contain a member with this name,
the given default value is returned. If this object contains multiple
members with the given name, the last one will be picked. If this
member's value does not represent a JSON number or if it cannot be
interpreted as Java int, an exception is thrown.name - the name of the member whose value is to be returneddefaultValue - the value to be returned if the requested member is missingpublic Long getLong(String name, Long defaultValue)
long value of the member with the specified name
in this object. If this object does not contain a member with this name,
the given default value is returned. If this object contains multiple
members with the given name, the last one will be picked. If this
member's value does not represent a JSON number or if it cannot be
interpreted as Java long, an exception is thrown.name - the name of the member whose value is to be returneddefaultValue - the value to be returned if the requested member is missingpublic Float getFloat(String name, Float defaultValue)
float value of the member with the specified
name in this object. If this object does not contain a member with this
name, the given default value is returned. If this object contains
multiple members with the given name, the last one will be picked. If
this member's value does not represent a JSON number or if it cannot be
interpreted as Java float, an exception is thrown.name - the name of the member whose value is to be returneddefaultValue - the value to be returned if the requested member is missingpublic Double getDouble(String name, Double defaultValue)
double value of the member with the specified
name in this object. If this object does not contain a member with this
name, the given default value is returned. If this object contains
multiple members with the given name, the last one will be picked. If
this member's value does not represent a JSON number or if it cannot be
interpreted as Java double, an exception is thrown.name - the name of the member whose value is to be returneddefaultValue - the value to be returned if the requested member is missingpublic Boolean getBoolean(String name, Boolean defaultValue)
boolean value of the member with the specified
name in this object. If this object does not contain a member with this
name, the given default value is returned. If this object contains
multiple members with the given name, the last one will be picked. If
this member's value does not represent a JSON true or
false value, an exception is thrown.name - the name of the member whose value is to be returneddefaultValue - the value to be returned if the requested member is missingpublic boolean getBoolean(String name)
public String getString(String name, String defaultValue)
String value of the member with the specified
name in this object. If this object does not contain a member with this
name, the given default value is returned. If this object contains
multiple members with the given name, the last one is picked. If this
member's value does not represent a JSON string, an exception is thrown.name - the name of the member whose value is to be returneddefaultValue - the value to be returned if the requested member is missingpublic int size()
public boolean isEmpty()
true if this object contains no members.true if this object contains no memberspublic List<String> names()
public Iterator<JSONObject.Member> iterator()
iterator 在接口中 Iterable<JSONObject.Member>public boolean isObject()
JSONValueJSONObject.public JSONObject asJSONObject()
JSONValueJSONObject, assuming that this value
represents a JSON object. If this is not the case, an exception is
thrown.asJSONObject 在类中 JSONValuepublic boolean equals(Object obj)
JSONValueObject.equals(Object).
Two JsonValues are considered equal if and only if they represent the same JSON text. As a consequence, two given JsonObjects may be different even though they contain the same set of names with the same values, but in a different order.
Copyright © 2016. All rights reserved.