JSONObject class

A modifiable set of name/value mappings. Names are unique, non-null strings. Values may be any mix of {@link JSONObject JSONObjects}, {@link JSONArray JSONArrays}, Strings, Booleans, Integers, Longs, Doubles or {@link #NULL}. Values may not be {@code null}, {@link Double#isNaN() NaNs}, {@link Double#isInfinite() infinities}, or of any type not listed here.

This class can coerce values to another type when requested.

This class can look up both mandatory and optional values:

  • Use getType() to retrieve a mandatory value. This fails with a {@code EasyJSONException} if the requested name has no value or if the value cannot be coerced to the requested type.
  • Use optType() to retrieve an optional value. This returns a system- or user-supplied default if the requested name has no value or if the value cannot be coerced to the requested type.

Warning: this class represents null in two incompatible ways: the standard Java {@code null} reference, and the sentinel value {@link JSONObject#NULL}. In particular, calling {@code put(name, null)} removes the named entry from the object but {@code put(name, JSONObject.NULL)} stores an entry whose value is {@code JSONObject.NULL}.

Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.

Constructors

JSONObject({String json, Map<String, Object> map})
Creates a new {@code JSONObject} with name/value mappings from the JSON string. [...]

Properties

hashCode int
The hash code for this object. [...]
read-only, inherited
nameValuePairs HashMap<String, Object>
read / write
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

accumulate(String name, Object value) JSONObject
Appends {@code value} to the array already mapped to {@code name}. If this object has no mapping for {@code name}, this inserts a new mapping. If the mapping exists but its value is not an array, the existing and new values are inserted in order into a new array which is itself mapped to {@code name}. In aggregate, this allows values to be added to a mapping one at a time. [...]
append(String name, Object value) JSONObject
Appends values to the array mapped to {@code name}. A new {@link JSONArray} mapping for {@code name} will be inserted if no mapping exists. If the existing mapping for {@code name} is not a {@link JSONArray}, a {@link EasyJSONException} will be thrown. [...]
checkName(String name) String
entrySet() Iterable<MapEntry<String, Object>>
get(String name) Object
Returns the value mapped by {@code name}, or throws if no such mapping exists. [...]
getBoolean(String name) bool
Returns the value mapped by {@code name} if it exists and is a boolean or can be coerced to a boolean, or throws otherwise. [...]
getDouble(String name) double
Returns the value mapped by {@code name} if it exists and is a double or can be coerced to a double, or throws otherwise. [...]
getHashMap() HashMap<String, Object>
getInt(String name) int
Returns the value mapped by {@code name} if it exists and is an int or can be coerced to an int, or throws otherwise. [...]
getJSONArray(String name) JSONArray
Returns the value mapped by {@code name} if it exists and is a {@code JSONArray}, or throws otherwise. [...]
getJSONObject(String name) JSONObject
Returns the value mapped by {@code name} if it exists and is a {@code JSONObject}, or throws otherwise. [...]
getString(String name) String
Returns the value mapped by {@code name} if it exists, coercing it if necessary, or throws if no such mapping exists. [...]
has(String name) bool
Returns true if this object has a mapping for {@code name}. The mapping may be {@link #NULL}.
isNull(String name) bool
Returns true if this object has no mapping for {@code name} or if it has a mapping whose value is {@link #NULL}.
keys() Iterable<String>
Returns an iterator of the {@code String} names in this object. The returned iterator supports {@link Iterator#remove() remove}, which will remove the corresponding mapping from this object. If this object is modified after the iterator is returned, the iterator's behavior is undefined. The order of the keys is undefined.
length() int
Returns the number of name/value mappings in this object.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
opt(String name) Object
Returns the value mapped by {@code name}, or null if no such mapping exists.
optBoolean(String name, bool fallback) bool
Returns the value mapped by {@code name} if it exists and is a boolean or can be coerced to a boolean, or {@code fallback} otherwise.
optDouble(String name, double fallback) double
Returns the value mapped by {@code name} if it exists and is a double or can be coerced to a double, or {@code fallback} otherwise.
optInt(String name, int fallback) int
Returns the value mapped by {@code name} if it exists and is an int or can be coerced to an int, or {@code fallback} otherwise.
optJSONArray(String name) JSONArray
Returns the value mapped by {@code name} if it exists and is a {@code JSONArray}, or null otherwise.
optJSONObject(String name) JSONObject
Returns the value mapped by {@code name} if it exists and is a {@code JSONObject}, or null otherwise.
optString(String name, String fallback) String
Returns the value mapped by {@code name} if it exists, coercing it if necessary, or {@code fallback} if no such mapping exists.
put(String name, Object value) JSONObject
Maps {@code name} to {@code value}, clobbering any existing name/value mapping with the same name. If the value is {@code null}, any existing mapping for {@code name} is removed. [...]
putOpt(String name, Object value) JSONObject
Equivalent to {@code put(name, value)} when both parameters are non-null; does nothing otherwise.
remove(String name) Object
Removes the named mapping if it exists; does nothing otherwise. [...]
toJSONArray(JSONArray names) JSONArray
Returns an array with the values corresponding to {@code names}. The array contains null for names that aren't mapped. This method returns null if {@code names} is either null or empty.
toPrettyString(int indentSpaces) String
Encodes this object as a human readable JSON string for debugging, such as: [...]
toString() String
Encodes this object as a compact JSON string, such as: [...]
override
writeTo(JSONStringer stringer) → void

Operators

operator ==(Object other) bool
The equality operator. [...]
inherited

Static Properties

NEGATIVE_ZERO double
final
NULL JSONObjectNULL
A sentinel value used to explicitly define a name with no value. Unlike {@code null}, names with this value: [...]
final

Static Methods

numberToString(num number) String
Encodes the number as a JSON string. [...]
quote(String data) String
Encodes {@code data} as a JSON string. This applies quotes and any necessary character escaping. [...]
wrap(Object o) Object
Wraps the given object if necessary. [...]