-
- All Implemented Interfaces:
-
java.io.Serializable
public class JSONObject implements Serializable
A modifiable set of name/value mappings. Names are unique, non-null strings. Values may be any mix of JSONObjects, JSONArrays, Strings, Booleans, Integers, Longs, Doubles or NULL. Values may not be
{@code null}, NaNs, infinities, or of any type not listed here.This class can coerce values to another type when requested.
- When the requested type is a boolean, strings will be coerced using a case-insensitive comparison to "true" and "false".
- When the requested type is a double, other Number types will be coerced using doubleValue. Strings that can be coerced using valueOf will be.
- When the requested type is an int, other Number types will be coerced using intValue. Strings that can be coerced using valueOf will be, and then cast to int.
- When the requested type is a long, other Number types will be coerced using longValue. Strings that can be coerced using valueOf will be, and then cast to long. This two-step conversion is lossy for very large values. For example, the string "9223372036854775806" yields the long 9223372036854775807.
- When the requested type is a String, other non-null values will be coerced using valueOf. Although null cannot be coerced, the sentinel value NULL is coerced to the string "null".
This class can look up both mandatory and optional values:
- Use
getType()to retrieve a mandatory value. This fails with a{@code JSONException}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 . 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.
-
-
Constructor Summary
Constructors Constructor Description JSONObject()Creates a {@code JSONObject}with no name/value mappings.JSONObject(Map copyFrom)Creates a new {@code JSONObject}by copying all name/value mappings fromthe given map.JSONObject(JSONTokener readFrom)Creates a new {@code JSONObject}with name/value mappings from the nextobject in the tokener.JSONObject(String json)Creates a new {@code JSONObject}with name/value mappings from the JSONstring.JSONObject(JSONObject copyFrom, Array<String> names)Creates a new {@code JSONObject}by copying mappings for the listed namesfrom the given object.JSONObject(JSONObject copyFrom)Creates a new {@code JSONObject}by copying mappings for given object
-
Method Summary
Modifier and Type Method Description intlength()Returns the number of name/value mappings in this object. JSONObjectput(String name, boolean value)Maps {@code name}to{@code value}, clobbering any existing name/valuemapping with the same name.JSONObjectput(String name, double value)Maps {@code name}to{@code value}, clobbering any existing name/valuemapping with the same name.JSONObjectput(String name, int value)Maps {@code name}to{@code value}, clobbering any existing name/valuemapping with the same name.JSONObjectput(String name, long value)Maps {@code name}to{@code value}, clobbering any existing name/valuemapping with the same name.JSONObjectput(String name, Object value)Maps {@code name}to{@code value}, clobbering any existing name/valuemapping with the same name.JSONObjectputOpt(String name, Object value)Equivalent to {@code put(name, value)}when both parameters are non-null;does nothing otherwise.JSONObjectaccumulate(String name, Object value)Appends {@code value}to the array already mapped to{@code name}.JSONObjectappend(String name, Object value)Appends values to the array mapped to {@code name}.Objectremove(String name)Removes the named mapping if it exists; does nothing otherwise. booleanisNull(String name)Returns true if this object has no mapping for {@code name}or if it hasa mapping whose value is NULL.booleanhas(String name)Returns true if this object has a mapping for {@code name}.booleanhasNonNull(String name)Returns true if this object has a mapping for {@code name}.Objectget(String name)Returns the value mapped by {@code name}, or throws if no such mapping exists.Objectopt(String name)Returns the value mapped by {@code name}, or null if no such mappingexists.booleangetBoolean(String name)Returns the value mapped by {@code name}if it exists and is a boolean orcan be coerced to a boolean, or throws otherwise.booleanoptBoolean(String name)Returns the value mapped by {@code name}if it exists and is a boolean orcan be coerced to a boolean, or false otherwise.booleanoptBoolean(String name, boolean fallback)Returns the value mapped by {@code name}if it exists and is a boolean orcan be coerced to a boolean, or{@code fallback}otherwise.BooleanreallyOptBoolean(String name, Boolean fallback)Returns the value mapped by {@code name}if it exists and is not null, coercing it ifnecessary, or{@code fallback}if no such mapping exists or is null.doublegetDouble(String name)Returns the value mapped by {@code name}if it exists and is a double orcan be coerced to a double, or throws otherwise.doubleoptDouble(String name)Returns the value mapped by {@code name}if it exists and is a double orcan be coerced to a double, or{@code NaN}otherwise.doubleoptDouble(String name, double fallback)Returns the value mapped by {@code name}if it exists and is a double orcan be coerced to a double, or{@code fallback}otherwise.DoublereallyOptDouble(String name, Double fallback)Returns the value mapped by {@code name}if it exists and is not null, coercing it ifnecessary, or{@code fallback}if no such mapping exists or is null.intgetInt(String name)Returns the value mapped by {@code name}if it exists and is an int orcan be coerced to an int, or throws otherwise.intoptInt(String name)Returns the value mapped by {@code name}if it exists and is an int orcan be coerced to an int, or 0 otherwise.intoptInt(String name, int fallback)Returns the value mapped by {@code name}if it exists and is an int orcan be coerced to an int, or{@code fallback}otherwise.IntegerreallyOptInteger(String name, Integer fallback)Returns the value mapped by {@code name}if it exists and is not null, coercing it ifnecessary, or{@code fallback}if no such mapping exists or is null.longgetLong(String name)Returns the value mapped by {@code name}if it exists and is a long orcan be coerced to a long, or throws otherwise.LongreallyOptLong(String name, Long fallback)Returns the value mapped by {@code name}if it exists and is not null, coercing it ifnecessary, or{@code fallback}if no such mapping exists or is null.longoptLong(String name)Returns the value mapped by {@code name}if it exists and is a long orcan be coerced to a long, or 0 otherwise.longoptLong(String name, long fallback)Returns the value mapped by {@code name}if it exists and is a long orcan be coerced to a long, or{@code fallback}otherwise.StringgetString(String name)Returns the value mapped by {@code name}if it exists, coercing it ifnecessary, or throws if no such mapping exists.StringoptString(String name)Returns the value mapped by {@code name}if it exists, coercing it ifnecessary, or the empty string if no such mapping exists.StringoptString(String name, String fallback)Returns the value mapped by {@code name}if it exists, coercing it ifnecessary, or{@code fallback}if no such mapping exists.StringreallyOptString(String name, String fallback)Returns the value mapped by {@code name}if it exists and is not null, coercing it ifnecessary, or{@code fallback}if no such mapping exists or is null.JSONArraygetJSONArray(String name)Returns the value mapped by {@code name}if it exists and is a{@code * JSONArray}, or throws otherwise.JSONArrayoptJSONArray(String name)Returns the value mapped by {@code name}if it exists and is a{@code * JSONArray}, or null otherwise.JSONObjectgetJSONObject(String name)Returns the value mapped by {@code name}if it exists and is a{@code * JSONObject}, or throws otherwise.JSONObjectoptJSONObject(String name)Returns the value mapped by {@code name}if it exists and is a{@code * JSONObject}, or null otherwise.JSONArraytoJSONArray(JSONArray names)Returns an array with the values corresponding to {@code names}.Iterator<String>keys()Returns an iterator of the {@code String}names in this object.Set<String>keySet()Returns the set of {@code String}names in this object.JSONArraynames()Returns an array containing the string names in this object. StringtoString()Encodes this object as a compact JSON string, such as: {"query":"Pizza","locations":[94043,90210]}StringtoString(int indentSpaces)Encodes this object as a human readable JSON string for debugging, suchas: { "query": "Pizza", "locations": [ 94043, 90210 ] }static StringnumberToString(Number number)Encodes the number as a JSON string. static Stringquote(String data)Encodes {@code data}as a JSON string.static Objectwrap(Object o)Wraps the given object if necessary. -
-
Constructor Detail
-
JSONObject
JSONObject()
Creates a{@code JSONObject}with no name/value mappings.
-
JSONObject
JSONObject(Map copyFrom)
Creates a new{@code JSONObject}by copying all name/value mappings fromthe given map.- Parameters:
copyFrom- a map whose keys are of type String and whosevalues are of supported types.
-
JSONObject
JSONObject(JSONTokener readFrom)
Creates a new{@code JSONObject}with name/value mappings from the nextobject in the tokener.- Parameters:
readFrom- a tokener whose nextValue() method will yield a{@code JSONObject}.
-
JSONObject
JSONObject(String json)
Creates a new{@code JSONObject}with name/value mappings from the JSONstring.- Parameters:
json- a JSON-encoded string containing an object.
-
JSONObject
JSONObject(JSONObject copyFrom, Array<String> names)
Creates a new{@code JSONObject}by copying mappings for the listed namesfrom the given object.
-
JSONObject
JSONObject(JSONObject copyFrom)
Creates a new{@code JSONObject}by copying mappings for given object
-
-
Method Detail
-
length
int length()
Returns the number of name/value mappings in this object.
-
put
JSONObject put(String name, boolean value)
Maps
{@code name}to{@code value}, clobbering any existing name/valuemapping with the same name.
-
put
JSONObject put(String name, double value)
Maps
{@code name}to{@code value}, clobbering any existing name/valuemapping with the same name.- Parameters:
value- a finite value.
-
put
JSONObject put(String name, int value)
Maps
{@code name}to{@code value}, clobbering any existing name/valuemapping with the same name.
-
put
JSONObject put(String name, long value)
Maps
{@code name}to{@code value}, clobbering any existing name/valuemapping with the same name.
-
put
JSONObject put(String name, Object value)
Maps
{@code name}to{@code value}, clobbering any existing name/valuemapping with the same name. If the value is{@code null}, any existingmapping for{@code name}is removed.- Parameters:
value- a JSONObject, JSONArray, String, Boolean,Integer, Long, Double, NULL, or{@code null}.
-
putOpt
JSONObject putOpt(String name, Object value)
Equivalent to
{@code put(name, value)}when both parameters are non-null;does nothing otherwise.
-
accumulate
JSONObject accumulate(String name, Object value)
Appends
{@code value}to the array already mapped to{@code name}. Ifthis object has no mapping for{@code name}, this inserts a new mapping.If the mapping exists but its value is not an array, the existingand new values are inserted in order into a new array which is itselfmapped to{@code name}. In aggregate, this allows values to be added to amapping one at a time.Note that
{@code append(String, Object)}provides better semantics.In particular, the mapping for{@code name}will always be a JSONArray. Using{@code accumulate}will result in either a JSONArray or a mapping whose type is the type of{@code value}depending on the number of calls to it.- Parameters:
value- a JSONObject, JSONArray, String, Boolean,Integer, Long, Double, NULL or null.
-
append
JSONObject append(String name, Object value)
Appends values to the array mapped to
{@code name}. A new JSONArray mapping for{@code name}will be inserted if no mapping exists. If the existingmapping for{@code name}is not a JSONArray, a JSONException will be thrown.
-
isNull
boolean isNull(String name)
Returns true if this object has no mapping for
{@code name}or if it hasa mapping whose value is NULL.
-
has
boolean has(String name)
Returns true if this object has a mapping for
{@code name}. The mappingmay be NULL.
-
hasNonNull
boolean hasNonNull(String name)
Returns true if this object has a mapping for
{@code name}. The mappingCANNOT be NULL.
-
get
Object get(String name)
Returns the value mapped by
{@code name}, or throws if no such mapping exists.
-
opt
Object opt(String name)
Returns the value mapped by
{@code name}, or null if no such mappingexists.
-
getBoolean
boolean getBoolean(String name)
Returns the value mapped by
{@code name}if it exists and is a boolean orcan be coerced to a boolean, or throws otherwise.
-
optBoolean
boolean optBoolean(String name)
Returns the value mapped by
{@code name}if it exists and is a boolean orcan be coerced to a boolean, or false otherwise.
-
optBoolean
boolean optBoolean(String name, boolean fallback)
Returns the value mapped by
{@code name}if it exists and is a boolean orcan be coerced to a boolean, or{@code fallback}otherwise.
-
reallyOptBoolean
Boolean reallyOptBoolean(String name, Boolean fallback)
Returns the value mapped by
{@code name}if it exists and is not null, coercing it ifnecessary, or{@code fallback}if no such mapping exists or is null.
-
getDouble
double getDouble(String name)
Returns the value mapped by
{@code name}if it exists and is a double orcan be coerced to a double, or throws otherwise.
-
optDouble
double optDouble(String name)
Returns the value mapped by
{@code name}if it exists and is a double orcan be coerced to a double, or{@code NaN}otherwise.
-
optDouble
double optDouble(String name, double fallback)
Returns the value mapped by
{@code name}if it exists and is a double orcan be coerced to a double, or{@code fallback}otherwise.
-
reallyOptDouble
Double reallyOptDouble(String name, Double fallback)
Returns the value mapped by
{@code name}if it exists and is not null, coercing it ifnecessary, or{@code fallback}if no such mapping exists or is null.
-
getInt
int getInt(String name)
Returns the value mapped by
{@code name}if it exists and is an int orcan be coerced to an int, or throws otherwise.
-
optInt
int optInt(String name)
Returns the value mapped by
{@code name}if it exists and is an int orcan be coerced to an int, or 0 otherwise.
-
optInt
int optInt(String name, int fallback)
Returns the value mapped by
{@code name}if it exists and is an int orcan be coerced to an int, or{@code fallback}otherwise.
-
reallyOptInteger
Integer reallyOptInteger(String name, Integer fallback)
Returns the value mapped by
{@code name}if it exists and is not null, coercing it ifnecessary, or{@code fallback}if no such mapping exists or is null.
-
getLong
long getLong(String name)
Returns the value mapped by
{@code name}if it exists and is a long orcan be coerced to a long, or throws otherwise.Note that JSON represents numbers as doubles,so this is lossy; use strings to transfer numbers via JSON.
-
reallyOptLong
Long reallyOptLong(String name, Long fallback)
Returns the value mapped by
{@code name}if it exists and is not null, coercing it ifnecessary, or{@code fallback}if no such mapping exists or is null.
-
optLong
long optLong(String name)
Returns the value mapped by
{@code name}if it exists and is a long orcan be coerced to a long, or 0 otherwise. Note that JSON represents numbers as doubles,so this is lossy; use strings to transfer numbers via JSON.
-
optLong
long optLong(String name, long fallback)
Returns the value mapped by
{@code name}if it exists and is a long orcan be coerced to a long, or{@code fallback}otherwise. Note that JSON representsnumbers as doubles, so this is lossy; use strings to transfernumbers via JSON.
-
getString
String getString(String name)
Returns the value mapped by
{@code name}if it exists, coercing it ifnecessary, or throws if no such mapping exists.
-
optString
String optString(String name)
Returns the value mapped by
{@code name}if it exists, coercing it ifnecessary, or the empty string if no such mapping exists.
-
optString
String optString(String name, String fallback)
Returns the value mapped by
{@code name}if it exists, coercing it ifnecessary, or{@code fallback}if no such mapping exists.
-
reallyOptString
String reallyOptString(String name, String fallback)
Returns the value mapped by
{@code name}if it exists and is not null, coercing it ifnecessary, or{@code fallback}if no such mapping exists or is null.
-
getJSONArray
JSONArray getJSONArray(String name)
Returns the value mapped by
{@code name}if it exists and is a{@code * JSONArray}, or throws otherwise.
-
optJSONArray
JSONArray optJSONArray(String name)
Returns the value mapped by
{@code name}if it exists and is a{@code * JSONArray}, or null otherwise.
-
getJSONObject
JSONObject getJSONObject(String name)
Returns the value mapped by
{@code name}if it exists and is a{@code * JSONObject}, or throws otherwise.
-
optJSONObject
JSONObject optJSONObject(String name)
Returns the value mapped by
{@code name}if it exists and is a{@code * JSONObject}, or null otherwise.
-
toJSONArray
JSONArray toJSONArray(JSONArray names)
Returns an array with the values corresponding to
{@code names}. Thearray contains null for names that aren't mapped. This method returnsnull if{@code names}is either null or empty.
-
keys
Iterator<String> keys()
Returns an iterator of the
{@code String}names in this object. Thereturned iterator supports remove, which willremove the corresponding mapping from this object. If this object ismodified after the iterator is returned, the iterator's behavior isundefined. The order of the keys is undefined.
-
names
JSONArray names()
Returns an array containing the string names in this object. This methodreturns null if this object contains no mappings.
-
toString
String toString()
Encodes this object as a compact JSON string, such as:
{"query":"Pizza","locations":[94043,90210]}
-
toString
String toString(int indentSpaces)
Encodes this object as a human readable JSON string for debugging, suchas:
{ "query": "Pizza", "locations": [ 94043, 90210 ] }- Parameters:
indentSpaces- the number of spaces to indent for each level ofnesting.
-
numberToString
static String numberToString(Number number)
Encodes the number as a JSON string.
- Parameters:
number- a finite value.
-
quote
static String quote(String data)
Encodes
{@code data}as a JSON string. This applies quotes and anynecessary character escaping.- Parameters:
data- the string to encode.
-
wrap
static Object wrap(Object o)
Wraps the given object if necessary.
If the object is null or , returns NULL.If the object is a
{@code JSONArray}or{@code JSONObject}, no wrapping is necessary.If the object is{@code NULL}, no wrapping is necessary.If the object is an array or{@code Collection}, returns an equivalent{@code JSONArray}.If the object is a{@code Map}, returns an equivalent{@code JSONObject}.If the object is a primitive wrapper type or{@code String}, returns the object.Otherwise if the object is from a{@code java}package, returns the result of{@code toString}.If wrapping fails, returns null.
-
-
-
-