程序包 com.sh.json
类 JSONObject
java.lang.Object
com.sh.json.AbstractJSON
com.sh.json.JSONObject
- 所有已实现的接口:
JSON,Serializable,Comparable,Map
A JSONObject is an unordered collection of name/value pairs.
- 另请参阅:
-
嵌套类概要
-
构造器概要
构造器构造器说明Construct an empty JSONObject.JSONObject(boolean isNull) Creates a JSONObject that is null.JSONObject(Map<String, Object> map) -
方法概要
修饰符和类型方法说明protected Object_processValue(Object value, JsonConfig jsonConfig) accumulate(String key, boolean value) Accumulate values under a key.accumulate(String key, double value) Accumulate values under a key.accumulate(String key, int value) Accumulate values under a key.accumulate(String key, long value) Accumulate values under a key.accumulate(String key, Object value) Accumulate values under a key.accumulate(String key, Object value, JsonConfig jsonConfig) Accumulate values under a key.voidaccumulateAll(Map map) voidaccumulateAll(Map map, JsonConfig jsonConfig) voidclear()intbooleancontainsKey(Object key) booleancontainsValue(Object value) booleancontainsValue(Object value, JsonConfig jsonConfig) Remove a name and its value, if present.Put a key/boolean pair in the JSONObject.Put a key/double pair in the JSONObject.Put a key/int pair in the JSONObject.Put a key/long pair in the JSONObject.Put a key/value pair in the JSONObject.element(String key, Object value, JsonConfig jsonConfig) Put a key/value pair in the JSONObject.element(String key, Collection value) Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection.element(String key, Collection value, JsonConfig jsonConfig) Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection.Put a key/value pair in the JSONObject, where the value will be a JSONObject which is produced from a Map.element(String key, Map value, JsonConfig jsonConfig) Put a key/value pair in the JSONObject, where the value will be a JSONObject which is produced from a Map.elementOpt(String key, Object value) Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.elementOpt(String key, Object value, JsonConfig jsonConfig) Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.entrySet()booleanfluentPut(Object key, Object value, JsonConfig config) static JSONObjectfromObject(Object object) Creates a JSONObject.
Inspects the object type to call the correct JSONObject factory method.static JSONObjectfromObject(Object object, JsonConfig jsonConfig) Creates a JSONObject.
Inspects the object type to call the correct JSONObject factory method.Get the value object associated with a key.booleangetBoolean(String key) Get the boolean value associated with a key.booleangetBoolean(String key, boolean defaultValue) doubleGet the double value associated with a key.doubleintGet the int value associated with a key.intgetInteger(String key) intgetIntValue(String key) getJSONArray(String key) Get the JSONArray value associated with a key.getJSONObject(String key) Get the JSONObject value associated with a key.longGet the long value associated with a key.longGet the string associated with a key.booleanDetermine if the JSONObject contains a specific key.inthashCode()booleanisArray()Returns true if this object is a JSONArray, false otherwise.booleanisEmpty()Returns true if this object has no elements or keys.booleanReturs if this object is a null JSONObject.keys()Get an enumeration of the keys of the JSONObject.keySet()names()Produce a JSONArray containing the names of the elements of this JSONObject.names(JsonConfig jsonConfig) Produce a JSONArray containing the names of the elements of this JSONObject.Get an optional value associated with a key.booleanoptBoolean(String key) Get an optional boolean associated with a key.booleanoptBoolean(String key, boolean defaultValue) Get an optional boolean associated with a key.doubledoubleGet an optional double associated with a key, or the defaultValue if there is no such key or if its value is not a number.intintoptJSONArray(String key) Get an optional JSONArray associated with a key.optJSONObject(String key) Get an optional JSONObject associated with a key.longGet an optional long value associated with a key, or zero if there is no such key or if the value is not a number.longGet an optional long value associated with a key, or the default if there is no such key or if the value is not a number.Get an optional string associated with a key.Get an optional string associated with a key.static JSONObjectparseObject(Object object) 兼容fastjsonput(Object key, Object value, JsonConfig config) voidvoidputAll(Map map, JsonConfig jsonConfig) Remove a name and its value, if present.intsize()Get the number of keys stored in the JSONObject.protected static ObjecttoBean(JSONObject jsonObject) Creates a JSONDynaBean from a JSONObject.protected static ObjecttoBean(JSONObject jsonObject, JsonConfig jsonConfig) Creates a bean from a JSONObject, with the specific configuration.static <T> TtoBean(JSONObject jsonObject, Class<T> beanClass) Creates a bean from a JSONObject, with a specific target class.protected static ObjecttoBean(JSONObject jsonObject, Class beanClass, Map classMap) Creates a bean from a JSONObject, with a specific target class.
If beanClass is null, this method will return a graph of DynaBeans.protected static ObjecttoBean(JSONObject jsonObject, Object root, JsonConfig jsonConfig) Creates a bean from a JSONObject, with the specific configuration.static <T> Tstatic JSONObject兼容fastjsontoJSONArray(JSONArray names) Produce a JSONArray containing the values of the members of this JSONObject.toString()Make a JSON text of this JSONObject.toString(int indentFactor) Make a prettyprinted JSON text of this JSONObject.toString(int indentFactor, int indent) Make a prettyprinted JSON text of this JSONObject.values()Write the contents of the JSONObject as JSON text to a writer.从类继承的方法 com.sh.json.AbstractJSON
addInstance, fireArrayEndEvent, fireArrayStartEvent, fireElementAddedEvent, fireErrorEvent, fireObjectEndEvent, fireObjectStartEvent, firePropertySetEvent, fireWarnEvent, removeInstance从接口继承的方法 java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
构造器详细资料
-
JSONObject
public JSONObject()Construct an empty JSONObject. -
JSONObject
-
JSONObject
public JSONObject(boolean isNull) Creates a JSONObject that is null.
-
-
方法详细资料
-
fromObject
Creates a JSONObject.
Inspects the object type to call the correct JSONObject factory method. Accepts JSON formatted strings, Maps, DynaBeans and JavaBeans.- 参数:
object-- 抛出:
JSONException- if the object can not be converted to a proper JSONObject.
-
parseObject
兼容fastjson- 参数:
object-- 返回:
-
toJSON
兼容fastjson- 参数:
object-- 返回:
-
fromObject
Creates a JSONObject.
Inspects the object type to call the correct JSONObject factory method. Accepts JSON formatted strings, Maps, DynaBeans and JavaBeans.- 参数:
object-- 抛出:
JSONException- if the object can not be converted to a proper JSONObject.
-
toBean
Creates a JSONDynaBean from a JSONObject. -
toBean
Creates a bean from a JSONObject, with a specific target class. -
toBean
-
toBean
Creates a bean from a JSONObject, with a specific target class.
If beanClass is null, this method will return a graph of DynaBeans. Any attribute that is a JSONObject and matches a key in the classMap will be converted to that target class.
The classMap has the following conventions:- Every key must be an String.
- Every value must be a Class.
- A key may be a regular expression.
-
toBean
Creates a bean from a JSONObject, with the specific configuration. -
toBean
Creates a bean from a JSONObject, with the specific configuration. -
accumulate
Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.- 参数:
key- A key string.value- An object to be accumulated under the key.- 返回:
- this.
- 抛出:
JSONException- If the value is an invalid number or if the key is null.
-
accumulate
Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.- 参数:
key- A key string.value- An object to be accumulated under the key.- 返回:
- this.
- 抛出:
JSONException- If the value is an invalid number or if the key is null.
-
accumulate
Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.- 参数:
key- A key string.value- An object to be accumulated under the key.- 返回:
- this.
- 抛出:
JSONException- If the value is an invalid number or if the key is null.
-
accumulate
Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.- 参数:
key- A key string.value- An object to be accumulated under the key.- 返回:
- this.
- 抛出:
JSONException- If the value is an invalid number or if the key is null.
-
accumulate
Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.- 参数:
key- A key string.value- An object to be accumulated under the key.- 返回:
- this.
- 抛出:
JSONException- If the value is an invalid number or if the key is null.
-
accumulate
Accumulate values under a key. It is similar to the element method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already a JSONArray, then the new value is appended to it. In contrast, the replace method replaces the previous value.- 参数:
key- A key string.value- An object to be accumulated under the key.- 返回:
- this.
- 抛出:
JSONException- If the value is an invalid number or if the key is null.
-
accumulateAll
-
accumulateAll
-
clear
public void clear() -
compareTo
- 指定者:
compareTo在接口中Comparable
-
containsKey
- 指定者:
containsKey在接口中Map
-
containsValue
- 指定者:
containsValue在接口中Map
-
containsValue
-
discard
Remove a name and its value, if present.- 参数:
key- A key string.- 返回:
- this.
-
element
Put a key/boolean pair in the JSONObject.- 参数:
key- A key string.value- A boolean which is the value.- 返回:
- this.
- 抛出:
JSONException- If the key is null.
-
element
Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection.- 参数:
key- A key string.value- A Collection value.- 返回:
- this.
- 抛出:
JSONException
-
element
Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection.- 参数:
key- A key string.value- A Collection value.- 返回:
- this.
- 抛出:
JSONException
-
element
Put a key/double pair in the JSONObject.- 参数:
key- A key string.value- A double which is the value.- 返回:
- this.
- 抛出:
JSONException- If the key is null or if the number is invalid.
-
element
Put a key/int pair in the JSONObject.- 参数:
key- A key string.value- An int which is the value.- 返回:
- this.
- 抛出:
JSONException- If the key is null.
-
element
Put a key/long pair in the JSONObject.- 参数:
key- A key string.value- A long which is the value.- 返回:
- this.
- 抛出:
JSONException- If the key is null.
-
element
Put a key/value pair in the JSONObject, where the value will be a JSONObject which is produced from a Map.- 参数:
key- A key string.value- A Map value.- 返回:
- this.
- 抛出:
JSONException
-
element
Put a key/value pair in the JSONObject, where the value will be a JSONObject which is produced from a Map.- 参数:
key- A key string.value- A Map value.- 返回:
- this.
- 抛出:
JSONException
-
element
Put a key/value pair in the JSONObject. If the value is null, then the key will be removed from the JSONObject if it is present.
If there is a previous value assigned to the key, it will call accumulate.- 参数:
key- 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 JSONNull object.- 返回:
- this.
- 抛出:
JSONException- If the value is non-finite number or if the key is null.
-
element
Put a key/value pair in the JSONObject. If the value is null, then the key will be removed from the JSONObject if it is present.
If there is a previous value assigned to the key, it will call accumulate.- 参数:
key- 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 JSONNull object.- 返回:
- this.
- 抛出:
JSONException- If the value is non-finite number or if the key is null.
-
elementOpt
Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.- 参数:
key- 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 JSONNull object.- 返回:
- this.
- 抛出:
JSONException- If the value is a non-finite number.
-
elementOpt
Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.- 参数:
key- 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 JSONNull object.- 返回:
- this.
- 抛出:
JSONException- If the value is a non-finite number.
-
entrySet
-
equals
-
get
-
get
Get the value object associated with a key.- 参数:
key- A key string.- 返回:
- The object associated with the key.
- 抛出:
JSONException- if this.isNull() returns true.
-
getBoolean
Get the boolean value associated with a key.- 参数:
key- A key string.- 返回:
- The truth.
- 抛出:
JSONException- if the value is not a Boolean or the String "true" or "false".
-
getBoolean
-
getDouble
Get the double value associated with a key.- 参数:
key- A key string.- 返回:
- The numeric value.
- 抛出:
JSONException- if the key is not found or if the value is not a Number object and cannot be converted to a number.
-
getDouble
-
getInt
Get the int value associated with a key. If the number value is too large for an int, it will be clipped.- 参数:
key- A key string.- 返回:
- The integer value.
- 抛出:
JSONException- if the key is not found or if the value cannot be converted to an integer.
-
getIntValue
-
getInt
-
getInteger
-
getJSONArray
Get the JSONArray value associated with a key.- 参数:
key- A key string.- 返回:
- A JSONArray which is the value.
- 抛出:
JSONException- if the key is not found or if the value is not a JSONArray.
-
getJSONObject
Get the JSONObject value associated with a key.- 参数:
key- A key string.- 返回:
- A JSONObject which is the value.
- 抛出:
JSONException- if the key is not found or if the value is not a JSONObject.
-
getLong
Get the long value associated with a key. If the number value is too long for a long, it will be clipped.- 参数:
key- A key string.- 返回:
- The long value.
- 抛出:
JSONException- if the key is not found or if the value cannot be converted to a long.
-
getLong
-
getString
Get the string associated with a key.- 参数:
key- A key string.- 返回:
- A string which is the value.
- 抛出:
JSONException- if the key is not found.
-
getString
-
getStr
-
getStr
-
has
Determine if the JSONObject contains a specific key.- 参数:
key- A key string.- 返回:
- true if the key exists in the JSONObject.
-
hashCode
public int hashCode() -
isArray
public boolean isArray()从接口复制的说明:JSONReturns true if this object is a JSONArray, false otherwise. -
isEmpty
public boolean isEmpty()从接口复制的说明:JSONReturns true if this object has no elements or keys. -
isNullObject
public boolean isNullObject()Returs if this object is a null JSONObject. -
keys
Get an enumeration of the keys of the JSONObject.- 返回:
- An iterator of the keys.
-
keySet
-
names
Produce a JSONArray containing the names of the elements of this JSONObject.- 返回:
- A JSONArray containing the key strings, or null if the JSONObject is empty.
-
names
Produce a JSONArray containing the names of the elements of this JSONObject.- 返回:
- A JSONArray containing the key strings, or null if the JSONObject is empty.
-
opt
Get an optional value associated with a key.- 参数:
key- A key string.- 返回:
- An object which is the value, or null if there is no value.
-
optBoolean
Get an optional boolean associated with a key. It returns false if there is no such key, or if the value is not Boolean.TRUE or the String "true".- 参数:
key- A key string.- 返回:
- The truth.
-
optBoolean
Get an optional boolean associated with a key. It returns the defaultValue if there is no such key, or if it is not a Boolean or the String "true" or "false" (case insensitive).- 参数:
key- A key string.defaultValue- The default.- 返回:
- The truth.
-
optDouble
-
optDouble
Get an optional double associated with a key, or the defaultValue if there is no such key or if its value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.- 参数:
key- A key string.defaultValue- The default.- 返回:
- An object which is the value.
-
optInt
-
optInt
-
optJSONArray
Get an optional JSONArray associated with a key. It returns null if there is no such key, or if its value is not a JSONArray.- 参数:
key- A key string.- 返回:
- A JSONArray which is the value.
-
optJSONObject
Get an optional JSONObject associated with a key. It returns null if there is no such key, or if its value is not a JSONObject.- 参数:
key- A key string.- 返回:
- A JSONObject which is the value.
-
optLong
Get an optional long value associated with a key, or zero if there is no such key or if the value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.- 参数:
key- A key string.- 返回:
- An object which is the value.
-
optLong
Get an optional long value associated with a key, or the default if there is no such key or if the value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.- 参数:
key- A key string.defaultValue- The default.- 返回:
- An object which is the value.
-
optString
Get an optional string associated with a key. It returns an empty string if there is no such key. If the value is not a string and is not null, then it is coverted to a string.- 参数:
key- A key string.- 返回:
- A string which is the value.
-
optString
Get an optional string associated with a key. It returns the defaultValue if there is no such key.- 参数:
key- A key string.defaultValue- The default.- 返回:
- A string which is the value.
-
put
-
put
-
fluentPut
-
fluentPut
-
putAll
-
putAll
-
remove
-
remove
Remove a name and its value, if present.- 参数:
key- The name to be removed.- 返回:
- The value that was associated with the name, or null if there was no value.
-
size
public int size()Get the number of keys stored in the JSONObject. -
toJSONArray
Produce a JSONArray containing the values of the members of this JSONObject.- 参数:
names- A JSONArray containing a list of key strings. This determines the sequence of the values in the result.- 返回:
- A JSONArray of values.
- 抛出:
JSONException- If any of the values are non-finite numbers.
-
toString
Make a JSON text of this JSONObject. For compactness, no whitespace is added. If this would not result in a syntactically correct JSON text, then null will be returned instead.Warning: This method assumes that the data structure is acyclical.
-
toJSONString
-
toString
Make a prettyprinted JSON text of this JSONObject.Warning: This method assumes that the data structure is acyclical.
- 指定者:
toString在接口中JSON- 参数:
indentFactor- The number of spaces to add to each level of indentation.- 返回:
- a printable, displayable, portable, transmittable representation
of the object, beginning with
{(left brace) and ending with}(right brace). - 抛出:
JSONException- If the object contains an invalid number.
-
toString
Make a prettyprinted JSON text of this JSONObject.Warning: This method assumes that the data structure is acyclical.
- 指定者:
toString在接口中JSON- 参数:
indentFactor- The number of spaces to add to each level of indentation.indent- The indentation of the top level.- 返回:
- a printable, displayable, transmittable representation of the
object, beginning with
{(left brace) and ending with}(right brace). - 抛出:
JSONException- If the object contains an invalid number.
-
values
-
write
Write the contents of the JSONObject as JSON text to a writer. For compactness, no whitespace is added.Warning: This method assumes that the data structure is acyclical.
- 指定者:
write在接口中JSON- 返回:
- The writer.
- 抛出:
JSONException
-
_processValue
- 覆盖:
_processValue在类中AbstractJSON
-