public class WritableJSONObject extends MapBasedJSONObject implements java.lang.Cloneable
get and opt methods
for accessing the values by name, and put methods for adding or replacing values by
name. The values can be any of these types: Boolean, JSONArray,
JSONObject, Number, String, or the
JSONObject.NULL object. A JSONObject constructor can be used to convert an external
form JSON text into an internal form whose values can be retrieved with the get and
opt methods, or to convert values into a JSON text using the put and
toString methods. A get method returns a value if one can be found, and
throws an exception if one cannot be found. An opt method returns a default value
instead of throwing an exception, and so is useful for obtaining optional values.
The generic get() and opt() methods return an object, which you can
cast or query for type. There are also typed get and opt methods that
do type checking and type coercion for you. The opt methods differ from the get methods in that
they do not throw. Instead, they return a specified value, such as null.
The put methods add or replace values in an object. For example,
myString = new JSONObject().put("JSON", "Hello, World!").toString();
produces the string {"JSON": "Hello, World"}.
The texts produced by the toString methods strictly conform to the JSON syntax
rules. The constructors are more forgiving in the texts they will accept:
, (comma) may appear just before the closing brace.
' (single quote).{ } [ ] / \ : , = ; # and if they do not look like numbers and if they
are not the reserved words true, false, or null.= or => as well as by :.; (semicolon) as well as by
, (comma).0x- (hex) prefix.| Modifier and Type | Class and Description |
|---|---|
static class |
WritableJSONObject.Builder |
EMPTY| Constructor and Description |
|---|
WritableJSONObject()
Construct an empty JSONObject.
|
WritableJSONObject(java.lang.String baseName,
java.util.Locale locale)
Construct a JSONObject from a ResourceBundle.
|
| Modifier and Type | Method and Description |
|---|---|
WritableJSONObject |
clone() |
static WritableJSONObject |
create(java.lang.String source) |
boolean |
equalsMap(java.util.Map<java.lang.String,java.lang.Object> map) |
WritableJSONArray |
getJSONArray(java.lang.String key)
Get the JSONArray value associated with a key.
|
WritableJSONObject |
getJSONObject(java.lang.String key)
Get the JSONObject value associated with a key.
|
static com.google.common.base.Supplier<WritableJSONObject.Builder> |
getJSONObjectBuilderSupplier() |
protected java.util.Map<java.lang.String,java.lang.Object> |
getMap() |
WritableJSONArray |
optJSONArray(java.lang.String key)
Get an optional JSONArray associated with a key.
|
WritableJSONObject |
optJSONObject(java.lang.String key)
Get an optional JSONObject associated with a key.
|
WritableJSONObject |
put(java.lang.String key,
boolean value)
Put a key/boolean pair in the JSONObject.
|
WritableJSONObject |
put(java.lang.String key,
double value)
Put a key/double pair in the JSONObject.
|
WritableJSONObject |
put(java.lang.String key,
int value)
Put a key/int pair in the JSONObject.
|
WritableJSONObject |
put(java.lang.String key,
long value)
Put a key/long pair in the JSONObject.
|
WritableJSONObject |
put(java.lang.String key,
java.lang.Object value)
Put a key/value pair in the JSONObject.
|
WritableJSONObject |
putOnce(java.lang.String key,
java.lang.Object value)
Put a key/value pair in the JSONObject, but only if the key and the value are both non-null,
and only if there is not already a member with that name.
|
WritableJSONObject |
putOpt(java.lang.String key,
java.lang.Object value)
Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.
|
java.lang.Object |
remove(java.lang.String key)
Remove a name and its value, if present.
|
clone, equals, get, getBoolean, getDouble, getDoubleObj, getInt, getLong, getString, has, hashCode, isNull, keys, length, opt, optBoolean, optBoolean, optDouble, optDouble, optDoubleObj, optInt, optInt, optInteger, optInteger, optLong, optLong, optString, optString, sortedKeys, toString, toString, toString, toString, writepublic WritableJSONObject()
public WritableJSONObject(java.lang.String baseName,
java.util.Locale locale)
throws JSONException
baseName - The ResourceBundle base name.locale - The Locale to load the ResourceBundle for.JSONException - If any JSONExceptions are detected.public static WritableJSONObject create(java.lang.String source) throws JSONException
JSONExceptionJSONBuilder.toJSONObject(String)public static com.google.common.base.Supplier<WritableJSONObject.Builder> getJSONObjectBuilderSupplier()
protected java.util.Map<java.lang.String,java.lang.Object> getMap()
getMap in class MapBasedJSONObjectpublic WritableJSONObject clone()
clone in class java.lang.Objectpublic boolean equalsMap(java.util.Map<java.lang.String,java.lang.Object> map)
equalsMap in interface JSONObjectpublic WritableJSONArray getJSONArray(java.lang.String key) throws JSONException
MapBasedJSONObjectgetJSONArray in interface JSONObjectgetJSONArray in class MapBasedJSONObjectkey - A key string.JSONException - if the key is not found or if the value is not a JSONArray.public WritableJSONObject getJSONObject(java.lang.String key) throws JSONException
getJSONObject in interface JSONObjectgetJSONObject in class MapBasedJSONObjectkey - A key string.JSONException - if the key is not found or if the value is not a JSONObject.public WritableJSONArray optJSONArray(java.lang.String key)
optJSONArray in interface JSONObjectoptJSONArray in class MapBasedJSONObjectkey - A key string.public WritableJSONObject optJSONObject(java.lang.String key)
optJSONObject in interface JSONObjectoptJSONObject in class MapBasedJSONObjectkey - A key string.public WritableJSONObject put(java.lang.String key, boolean value) throws JSONException
put in interface JSONObjectkey - A key string.value - A boolean which is the value.JSONException - If the key is null.public WritableJSONObject put(java.lang.String key, double value) throws JSONException
put in interface JSONObjectkey - A key string.value - A double which is the value.JSONException - If the key is null or if the number is invalid.public WritableJSONObject put(java.lang.String key, int value) throws JSONException
put in interface JSONObjectkey - A key string.value - An int which is the value.JSONException - If the key is null.public WritableJSONObject put(java.lang.String key, long value) throws JSONException
put in interface JSONObjectkey - A key string.value - A long which is the value.JSONException - If the key is null.public WritableJSONObject put(java.lang.String key, java.lang.Object value) throws JSONException
put in interface JSONObjectkey - A key string.value - An object which is the value. It should be of one of these types: Boolean, Double,
Integer, JSONArray, JSONObject, Long, String, or the JSONObject.NULL object.JSONException - If the value is non-finite number or if the key is null.public WritableJSONObject putOnce(java.lang.String key, java.lang.Object value) throws JSONException
putOnce in interface JSONObjectkey - value - JSONException - if the key is a duplicatepublic WritableJSONObject putOpt(java.lang.String key, java.lang.Object value) throws JSONException
putOpt in interface JSONObjectkey - A key string.value - An object which is the value. It should be of one of these types: Boolean, Double,
Integer, JSONArray, JSONObject, Long, String, or the JSONObject.NULL object.JSONException - If the value is a non-finite number.public java.lang.Object remove(java.lang.String key)
remove in interface JSONObjectkey - The name to be removed.