public class JSONObject extends JSONElement
Modifier and Type | Field and Description |
---|---|
static Object |
NULL |
MAPPER
Constructor and Description |
---|
JSONObject()
an empty JSON object
|
JSONObject(Map<String,Object> map)
construct using a map
|
JSONObject(String string)
https://tools.ietf.org/html/rfc7159#section-4
|
Modifier and Type | Method and Description |
---|---|
JSONObject |
accumulate(String key,
Object additionalValue)
Add a element to a JSONArray in a element.
|
JSONObject |
append(String key,
Object value)
appends to an existing array
|
static String |
doubleToString(double d)
convert a primitive number to a string
if the double can be converted to a whole number the decimal will be dropped
|
boolean |
equals(Object other) |
Object |
get(String key)
get and element by key as its native object
|
BigDecimal |
getBigDecimal(String key)
get an element property as a BigDecimal
|
BigInteger |
getBigInteger(String key)
get an element property as a BigInteger
|
boolean |
getBoolean(String key)
gets a boolean value at a particular key
|
double |
getDouble(String key)
get the value as a double
|
<T extends Enum<T>> |
getEnum(Class<T> enumClass,
String key)
get element as a enum value
|
float |
getFloat(String key)
get the value as a float
|
int |
getInt(String key)
get an element property as a int
|
JSONArray |
getJSONArray(String key)
get the element as a JSONArray
|
JSONObject |
getJSONObject(String key)
get the element as a JSONObject
|
long |
getLong(String key)
get the value as a long
|
static String[] |
getNames(JSONObject jsonObject)
get all of the keys of a JSONObject
|
static String[] |
getNames(Object o)
get all of the keys of a JSONObject or a empty array if not an JSONObject
|
Number |
getNumber(String key)
get an element property as a Number
|
String |
getString(String key)
get a element property as a string
|
boolean |
has(String key) |
int |
hashCode() |
JSONObject |
increment(String key)
increments a numeric value by 1, or creates it with a value of 1 if
it does not exist.
|
boolean |
isEmpty() |
boolean |
isNull(String key)
indicate if the key does not exist or its value is null
|
Iterator<String> |
keys()
get a iterator for the keyset
|
Set<String> |
keySet()
get all the keys as a set
|
int |
length() |
JSONArray |
names()
get the key names as a JSONArray
|
static String |
numberToString(Number number)
Convert a number to a string
|
Object |
opt(String key)
optionally return the object or null if it doesn't exist
|
BigDecimal |
optBigDecimal(String key,
BigDecimal defaultValue)
get the value as a BigDecimal or default value
|
BigInteger |
optBigInteger(String key,
BigInteger defaultValue)
get the value as a BigInteger or default value
|
boolean |
optBoolean(String key)
gets a boolean value at a particular key or false as default
|
boolean |
optBoolean(String key,
boolean defaultValue)
gets a boolean value at a particular key or a default value
|
double |
optDouble(String key)
the value as double or NaN
|
double |
optDouble(String key,
double defaultValue)
get the value as a double or default value
|
<T extends Enum<T>> |
optEnum(Class<T> enumClass,
String key)
get element as a enum value or null if the value cannot be mapped
|
<T extends Enum<T>> |
optEnum(Class<T> enumClass,
String key,
T defaultValue)
get element as a enum value or a default value if the value cannot be mapped
|
float |
optFloat(String key)
the value as double or NaN
|
float |
optFloat(String key,
float defaultValue)
get the value as a float or default value
|
int |
optInt(String key)
the value as int or NaN
|
int |
optInt(String key,
int defaultValue)
get the value as a int or default value
|
JSONArray |
optJSONArray(String key)
optionally get the element as a JSONArray
|
JSONObject |
optJSONObject(String key)
get the element as a JSONObject
|
long |
optLong(String key)
the value as long or NaN
|
long |
optLong(String key,
long defaultValue)
get the value as a long or default value
|
Number |
optNumber(String key)
the value as int or 0
|
Number |
optNumber(String key,
Number defaultValue)
get the value as a Number or default value
|
String |
optString(String key)
get a element property as a string
|
String |
optString(String key,
String defaultValue)
get a element property as a string
|
JSONObject |
put(String key,
boolean value)
put a boolean at a particular key
|
JSONObject |
put(String key,
Collection value)
put a Collection as a JSONArray at a particular key
|
JSONObject |
put(String key,
double value)
put a double at a particular key
|
JSONObject |
put(String key,
float value)
put a float at a particular key
|
JSONObject |
put(String key,
int value)
put a int at a particular key
|
JSONObject |
put(String key,
JSONArray array)
put a JSONArray at a particular key
|
JSONObject |
put(String key,
JSONObject object)
put a JSONObject at a particular key
|
JSONObject |
put(String key,
long value)
put a long at a particular key
|
JSONObject |
put(String key,
Map value)
put a Collection as a JSONArray at a particular key
|
JSONObject |
put(String key,
Number value)
put a Number at a particular key
|
JSONObject |
put(String key,
Object value)
put an object to a key.
|
JSONObject |
put(String key,
String value)
put a String at a particular key
|
<T extends Enum<T>> |
put(String key,
T enumvalue)
put a enum at a particular key.
|
JSONObject |
putOnce(String key,
Object value)
put a value to a key only if it does not exist
|
JSONObject |
putOpt(String key,
Object value)
optional put a value at a key as long as both they key and value are not null
otherwise it does nothing
|
static String |
quote(String s)
quite escape a string
|
static Writer |
quote(String s,
Writer writer)
quite escape a string
|
Object |
remove(String key)
remove a element by key name
|
boolean |
similar(Object o)
indicates if a JSONObject has the same elements as another JSONObject
|
static Object |
stringToValue(String str)
convert a primitive JSON type in a string (bool, number, null) to its primitive type
all decimal types will become doubles
|
JSONArray |
toJSONArray(JSONArray names)
creates an array of the values for they keys you provide
|
Map<String,Object> |
toMap()
converts this object to a map
|
String |
toString() |
String |
toString(int i)
render the object as a JSON String
|
static String |
valueToString(Object o)
Converts an object to a JSON String
|
static Object |
wrap(Object obj)
Convert an object to a object that can be added to a JSONElement
If the object is null return the NULL object
If the object is primitive return the original object
If the object is a map convert it to a JSONObject
If the object is a Collection or array return a JSONArray
If the object is anything else return a empty JSON Object
|
public static final Object NULL
public JSONObject(String string)
string
- a json object stringpublic JSONObject(Map<String,Object> map)
map
- a map representing the elements of a JSON Objectpublic JSONObject()
public static String quote(String s)
s
- a stringpublic static Writer quote(String s, Writer writer) throws IOException
s
- a stringwriter
- a writer to write the string toIOException
- if some IO thing goes wrongpublic static Object stringToValue(String str)
str
- a stringpublic static Object wrap(Object obj)
obj
- the objectpublic static String doubleToString(double d)
d
- a doublepublic static String numberToString(Number number) throws JSONException
number
- the number to convertJSONException
- if something goes wrongpublic static String valueToString(Object o) throws JSONException
o
- any objectJSONException
- if something goes wrongpublic static String[] getNames(JSONObject jsonObject)
jsonObject
- a JSONObjectpublic static String[] getNames(Object o)
o
- a Objectpublic String toString()
public String toString(int i) throws JSONException
i
- (ignored due to limitations in gson which uses a hardcoded indentation)JSONException
public boolean similar(Object o)
o
- another objectpublic boolean has(String key)
key
- the key element to operate onpublic int length()
public Object get(String key) throws JSONException
key
- the key element to operate onJSONException
- if the key does not existpublic JSONObject getJSONObject(String key) throws JSONException
key
- the key element to operate onJSONException
- if it is not a object or the key does not existpublic JSONObject optJSONObject(String key)
key
- the key element to operate onpublic JSONArray getJSONArray(String key) throws JSONException
key
- the key element to operate onJSONException
- if it is not an array or the key does not existpublic JSONArray optJSONArray(String key)
key
- the key element to operate onpublic String getString(String key) throws JSONException
key
- the key element to operate onJSONException
- if the key does not existpublic String optString(String key)
key
- the key element to operate onpublic String optString(String key, String defaultValue)
key
- the key element to operate ondefaultValue
- default value if the key does not exist or cannot be converted to a stringpublic double getDouble(String key) throws JSONException
key
- the key element to operate onJSONException
- if the object is not a number or does not existpublic double optDouble(String key)
key
- the key element to operate onpublic double optDouble(String key, double defaultValue)
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not validpublic float getFloat(String key) throws JSONException
key
- the key element to operate onJSONException
- if the object is not a number or does not existpublic float optFloat(String key)
key
- the key element to operate onpublic float optFloat(String key, float defaultValue)
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not validpublic long getLong(String key) throws JSONException
key
- the key element to operate onJSONException
- if the object is not a number or does not existpublic long optLong(String key)
key
- the key element to operate onpublic long optLong(String key, long defaultValue)
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not validpublic Number getNumber(String key) throws JSONException
key
- the key element to operate onJSONException
- if it is not a number or the key does not existpublic Number optNumber(String key)
key
- the key element to operate onpublic Number optNumber(String key, Number defaultValue)
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not validpublic int getInt(String key) throws JSONException
key
- the key element to operate onJSONException
- if it is not a number or the key does not existpublic int optInt(String key)
key
- the key element to operate onpublic int optInt(String key, int defaultValue)
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not validpublic BigInteger getBigInteger(String key) throws JSONException
key
- the key element to operate onJSONException
- if it is not a number or the key does not existpublic BigInteger optBigInteger(String key, BigInteger defaultValue)
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not validpublic BigDecimal getBigDecimal(String key) throws JSONException
key
- the key element to operate onJSONException
- if it is not a number or the key does not existpublic BigDecimal optBigDecimal(String key, BigDecimal defaultValue)
key
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not validpublic boolean getBoolean(String key) throws JSONException
key
- the keyJSONException
- if the element does not exist or is not a booleanpublic boolean optBoolean(String key)
key
- the keypublic boolean optBoolean(String key, boolean defaultValue)
key
- the keydefaultValue
- a default value if the key does not exist or value is not a booleanpublic <T extends Enum<T>> T getEnum(Class<T> enumClass, String key) throws JSONException
T
- the type of enum you wantenumClass
- a enum classkey
- the key element to operate onJSONException
- if it does not map to a enum of T or the key does not existpublic <T extends Enum<T>> T optEnum(Class<T> enumClass, String key)
T
- the type of enum you wantenumClass
- a enum classkey
- the key element to operate onpublic <T extends Enum<T>> T optEnum(Class<T> enumClass, String key, T defaultValue)
T
- the type of enum you wantenumClass
- a enum classkey
- the key element to operate ondefaultValue
- the default value to return if the index or value type are not validpublic JSONObject put(String key, JSONObject object) throws JSONException
key
- the key element to operate onobject
- JSONObjectJSONException
public JSONObject put(String key, JSONArray array) throws JSONException
key
- the key element to operate onarray
- JSONArrayJSONException
public JSONObject put(String key, boolean value) throws JSONException
key
- the key element to operate onvalue
- the boolean value to putJSONException
- if something goes wrongpublic JSONObject put(String key, Number value) throws JSONException
key
- the key element to operate onvalue
- NumberJSONException
public JSONObject put(String key, double value) throws JSONException
key
- the key element to operate onvalue
- doubleJSONException
- if something goes wrongpublic JSONObject put(String key, float value) throws JSONException
key
- the key element to operate onvalue
- floatJSONException
- if something goes wrongpublic JSONObject put(String key, long value) throws JSONException
key
- the key element to operate onvalue
- longJSONException
- if something goes wrongpublic JSONObject put(String key, int value) throws JSONException
key
- the key element to operate onvalue
- intJSONException
- if something goes wrongpublic JSONObject put(String key, String value) throws JSONException
key
- the key element to operate onvalue
- NumberJSONException
public JSONObject put(String key, Collection value) throws JSONException
key
- the key element to operate onvalue
- CollectionJSONException
public JSONObject put(String key, Map value) throws JSONException
key
- the key element to operate onvalue
- CollectionJSONException
public <T extends Enum<T>> JSONObject put(String key, T enumvalue) throws JSONException
T
- a type of enumkey
- the key element to operate onenumvalue
- a enumJSONException
- if something goes wrongpublic Object remove(String key)
key
- the key element to operate onpublic JSONObject accumulate(String key, Object additionalValue) throws JSONException
key
- the key element to operate onadditionalValue
- value to append to the arrayJSONException
public JSONObject append(String key, Object value) throws JSONException
key
- the key element to operate onvalue
- the object to putJSONException
- if the value exists and is not an arraypublic JSONObject increment(String key) throws JSONException
key
- the key element to operate onJSONException
- if something goes wrongpublic JSONObject putOnce(String key, Object value) throws JSONException
key
- the key element to operate onvalue
- the object to putJSONException
- if the key exists.public JSONObject put(String key, Object value) throws JSONException
key
- the key element to operate onvalue
- the object to putJSONException
- if something goes wrongpublic JSONObject putOpt(String key, Object value) throws JSONException
key
- the key element to operate onvalue
- the object to putJSONException
- if something goes wrongpublic JSONArray names()
public JSONArray toJSONArray(JSONArray names) throws JSONException
names
- a list of keys you want an array forJSONException
- if something goes wrongpublic Object opt(String key)
key
- the keypublic boolean isEmpty()
public boolean isNull(String key)
key
- the keyCopyright © 2020. All rights reserved.