Package com.alibaba.fastjson2
Class JSONObject
- All Implemented Interfaces:
Serializable,Cloneable,InvocationHandler,Map<String,Object>
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Constructor Summary
ConstructorsConstructorDescriptiondefaultJSONObject(int initialCapacity) JSONObject(int initialCapacity, float loadFactor) JSONObject(int initialCapacity, float loadFactor, boolean accessOrder) JSONObject(Map map) -
Method Summary
Modifier and TypeMethodDescriptionclone()booleancontainsKey(Object key) Returns true if this map contains a mapping for the specified keybooleancontainsKey(String key) Returns true if this map contains a mapping for the specified keyChained addition of elementsstatic JSONObjectSeeJSON.toJSON(java.lang.Object)for detailsstatic JSONObjectfrom(Object obj, JSONWriter.Feature... writeFeatures) SeeJSON.toJSON(java.lang.Object)for detailsReturns the Object of the associated keys in thisJSONObject.Returns the Object of the associated keys in thisJSONObject.getBigDecimal(String key) Returns theBigDecimalof the associated keys in thisJSONObject.getBigInteger(String key) Returns theBigIntegerof the associated keys in thisJSONObject.getBoolean(String key) Returns theBooleanof the associated keys in thisJSONObject.booleangetBooleanValue(String key) Returns a boolean value of the associated key in this object.booleangetBooleanValue(String key, boolean defaultValue) Returns a boolean value of the associated key in this object.Returns theByteof the associated keys in thisJSONObject.byte[]bytegetByteValue(String key) Returns a byte value of the associated keys in thisJSONObject.Returns theDateof the associated keys in thisJSONObject.Returns theDoubleof the associated keys in thisJSONObject.doublegetDoubleValue(String key) Returns a double value of the associated keys in thisJSONObject.Returns theFloatof the associated keys in thisJSONObject.floatgetFloatValue(String key) Returns a float value of the associated keys in thisJSONObject.getInstant(String key) Returns theBigIntegerof the associated keys in thisJSONObject.getInteger(String key) Returns theIntegerof the associated keys in thisJSONObject.intgetIntValue(String key) Returns an int value of the associated keys in thisJSONObject.intgetIntValue(String key, int defaultValue) Returns an int value of the associated keys in thisJSONObject.getJSONArray(String key) Returns theJSONArrayof the associated keys in thisJSONObject.getJSONObject(String key) Returns theJSONObjectof the associated keys in thisJSONObject.<T> List<T>getList(String key, Class<T> itemClass, JSONReader.Feature... features) Returns theLongof the associated keys in thisJSONObject.longgetLongValue(String key) Returns a long value of the associated keys in thisJSONObject.longgetLongValue(String key, long defaultValue) Returns a long value of the associated keys in thisJSONObject.<T> TgetObject(String key, TypeReference<T> typeReference, JSONReader.Feature... features) Returns the result of theTypeconverter conversion of the associated value in thisJSONObject.<T> TgetObject(String key, Class<T> type, JSONReader.Feature... features) Returns the result of theTypeconverter conversion of the associated value in thisJSONObject.<T> TgetObject(String key, Type type, JSONReader.Feature... features) Returns the result of theTypeconverter conversion of the associated value in thisJSONObject.<T> TgetObject(String key, Function<JSONObject, T> creator) getOrDefault(Object key, Object defaultValue) getOrDefault(String key, Object defaultValue) Returns theShortof the associated keys in thisJSONObject.shortgetShortValue(String key) Returns a short value of the associated keys in thisJSONObject.intif value instance of Map or Collection, return size, other return 0Returns theStringof the associated keys in thisJSONObject.booleanisValid(JSONSchema schema) voidnameFilter(NameFilter nameFilter) static JSONObjectof()JSONObject jsonObject = JSONObject.of();static JSONObjectPack a pair of key-values asJSONObjectstatic JSONObjectPack two key-value pairs asJSONObjectstatic JSONObjectPack three key-value pairs asJSONObjectstatic JSONObjectPack three key-value pairs asJSONObjectstatic JSONObjectof(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5) Pack three key-value pairs asJSONObjectstatic JSONObjectparse(String text, JSONReader.Feature... features) SeeJSON.parse(java.lang.String)for detailsstatic JSONObjectparseObject(String text) SeeJSON.parseObject(java.lang.String)for detailsstatic <T> TparseObject(String text, TypeReference<T> typeReference, JSONReader.Feature... features) SeeJSON.parseObject(java.lang.String)for detailsstatic <T> TparseObject(String text, Class<T> objectClass) SeeJSON.parseObject(java.lang.String)for detailsstatic <T> TparseObject(String text, Class<T> objectClass, JSONReader.Feature... features) SeeJSON.parseObject(java.lang.String)for detailsstatic <T> TparseObject(String text, Type objectType, JSONReader.Feature... features) SeeJSON.parseObject(java.lang.String)for details<T> Tto(TypeReference<T> typeReference, JSONReader.Feature... features) Convert thisJSONObjectto the specified Object<T> Tto(Class<T> clazz, JSONReader.Feature... features) Convert thisJSONObjectto the specified Object<T> Tto(Type type, JSONReader.Feature... features) Convert thisJSONObjectto the specified Object<T> Tto(Function<JSONObject, T> function) <T> TtoJavaObject(TypeReference<T> typeReference, JSONReader.Feature... features) Deprecated.<T> TtoJavaObject(Class<T> clazz, JSONReader.Feature... features) Convert thisJSONObjectto the specified Object<T> TtoJavaObject(Type type, JSONReader.Feature... features) Deprecated.since 2.0.4, please useto(Type, JSONReader.Feature...)byte[]toJSONBBytes(JSONWriter.Feature... features) Serialize to JSONB bytestoJSONString(JSONWriter.Feature... features) Serialize to JSONStringstatic StringtoJSONString(Object object, JSONWriter.Feature... features) Serialize Java Object to JSONStringwith specifiedJSONReader.Features enabledtoString()Serialize to JSONStringtoString(JSONWriter.Feature... features) Serialize to JSONStringvoidvalueFilter(ValueFilter valueFilter) Methods inherited from class java.util.LinkedHashMap
clear, containsValue, entrySet, forEach, keySet, removeEldestEntry, replaceAll, valuesMethods inherited from class java.util.HashMap
compute, computeIfAbsent, computeIfPresent, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, sizeMethods inherited from class java.util.AbstractMap
equals, hashCode
-
Constructor Details
-
JSONObject
public JSONObject()default -
JSONObject
public JSONObject(int initialCapacity) - Parameters:
initialCapacity- the initial capacity = (number of elements to store / load factor) + 1- Throws:
IllegalArgumentException- If the initial capacity is negative
-
JSONObject
public JSONObject(int initialCapacity, float loadFactor) - Parameters:
initialCapacity- the initial capacity = (number of elements to store / load factor) + 1loadFactor- the load factor- Throws:
IllegalArgumentException- If the initial capacity is negative or the load factor is negative- Since:
- 2.0.2
-
JSONObject
public JSONObject(int initialCapacity, float loadFactor, boolean accessOrder) - Parameters:
initialCapacity- the initial capacity = (number of elements to store / load factor) + 1loadFactor- the load factoraccessOrder- the ordering mode - true for access-order, false for insertion-order- Throws:
IllegalArgumentException- If the initial capacity is negative or the load factor is negative- Since:
- 2.0.2
-
JSONObject
- Parameters:
map- the map whose mappings are to be placed in this map- Throws:
NullPointerException- If the specified map is null
-
-
Method Details
-
get
Returns the Object of the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned
-
get
Returns the Object of the associated keys in thisJSONObject. -
getByPath
-
containsKey
Returns true if this map contains a mapping for the specified key- Parameters:
key- the key whose presence in this map is to be tested
-
containsKey
Returns true if this map contains a mapping for the specified key- Specified by:
containsKeyin interfaceMap<String,Object> - Overrides:
containsKeyin classHashMap<String,Object> - Parameters:
key- the key whose presence in this map is to be tested
-
getOrDefault
- Parameters:
key- the key whose associated value is to be returneddefaultValue- the default mapping of the key
-
getOrDefault
- Specified by:
getOrDefaultin interfaceMap<String,Object> - Overrides:
getOrDefaultin classLinkedHashMap<String,Object> - Parameters:
key- the key whose associated value is to be returneddefaultValue- the default mapping of the key- Since:
- 2.0.2
-
getJSONArray
Returns theJSONArrayof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
JSONArrayor null
-
getList
-
getJSONObject
Returns theJSONObjectof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
JSONObjector null
-
getString
Returns theStringof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
Stringor null
-
getDouble
Returns theDoubleof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
Doubleor null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable doubleJSONException- Unsupported type conversion toDouble
-
getDoubleValue
Returns a double value of the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
- double
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable doubleJSONException- Unsupported type conversion to double value
-
getFloat
Returns theFloatof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
Floator null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable floatJSONException- Unsupported type conversion toFloat
-
getFloatValue
Returns a float value of the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
- float
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable floatJSONException- Unsupported type conversion to float value
-
getLong
Returns theLongof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
Longor null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable longJSONException- Unsupported type conversion toLong
-
getLongValue
Returns a long value of the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
- long
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable longJSONException- Unsupported type conversion to long value
-
getLongValue
Returns a long value of the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returneddefaultValue- the default mapping of the key- Returns:
- long
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable longJSONException- Unsupported type conversion to long value
-
getInteger
Returns theIntegerof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
Integeror null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable intJSONException- Unsupported type conversion toInteger
-
getIntValue
Returns an int value of the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
- int
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable intJSONException- Unsupported type conversion to int value
-
getIntValue
Returns an int value of the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returneddefaultValue- the default mapping of the key- Returns:
- int
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable intJSONException- Unsupported type conversion to int value
-
getShort
Returns theShortof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
Shortor null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable shortJSONException- Unsupported type conversion toShort
-
getShortValue
Returns a short value of the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
- short
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable shortJSONException- Unsupported type conversion to short value
-
getByte
Returns theByteof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
Byteor null- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable byteJSONException- Unsupported type conversion toByte
-
getByteValue
Returns a byte value of the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
- byte
- Throws:
NumberFormatException- If the value of get isStringand it contains no parsable byteJSONException- Unsupported type conversion to byte value
-
getBytes
-
getBoolean
Returns theBooleanof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
Booleanor null- Throws:
JSONException- Unsupported type conversion toBoolean
-
getBooleanValue
Returns a boolean value of the associated key in this object.- Parameters:
key- the key whose associated value is to be returned- Returns:
- boolean
- Throws:
JSONException- Unsupported type conversion to boolean value
-
getBooleanValue
Returns a boolean value of the associated key in this object.- Parameters:
key- the key whose associated value is to be returneddefaultValue- the default mapping of the key- Returns:
- boolean
- Throws:
JSONException- Unsupported type conversion to boolean value
-
getBigInteger
Returns theBigIntegerof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
BigIntegeror null- Throws:
JSONException- Unsupported type conversion toBigIntegerNumberFormatException- If the value of get isStringand it is not a valid representation ofBigInteger
-
getBigDecimal
Returns theBigDecimalof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
BigDecimalor null- Throws:
JSONException- Unsupported type conversion toBigDecimalNumberFormatException- If the value of get isStringand it is not a valid representation ofBigDecimal
-
getDate
Returns theDateof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
Dateor null
-
getDate
- Since:
- 2.0.27
-
getInstant
Returns theBigIntegerof the associated keys in thisJSONObject.- Parameters:
key- the key whose associated value is to be returned- Returns:
BigIntegeror null
-
toString
Serialize to JSONString- Overrides:
toStringin classAbstractMap<String,Object> - Returns:
- JSON
String
-
toString
Serialize to JSONString- Parameters:
features- features to be enabled in serialization- Returns:
- JSON
String
-
toJSONString
Serialize to JSONString- Parameters:
features- features to be enabled in serialization- Returns:
- JSON
String
-
toJSONString
Serialize Java Object to JSONStringwith specifiedJSONReader.Features enabled- Parameters:
object- Java Object to be serialized into JSONStringfeatures- features to be enabled in serialization- Since:
- 2.0.6
-
toJSONBBytes
Serialize to JSONB bytes- Parameters:
features- features to be enabled in serialization- Returns:
- JSONB bytes
-
to
- Since:
- 2.0.4
-
to
Convert thisJSONObjectto the specified ObjectJSONObject obj = ... Map<String, User> users = obj.to(new TypeReference<HashMap<String, User>>(){}.getType());- Parameters:
type- specify theTypeto be convertedfeatures- features to be enabled in parsing- Since:
- 2.0.4
-
to
Convert thisJSONObjectto the specified ObjectJSONObject obj = ... Map<String, User> users = obj.to(new TypeReference<HashMap<String, User>>(){});- Parameters:
typeReference- specify theTypeReferenceto be convertedfeatures- features to be enabled in parsing- Since:
- 2.0.7
-
to
Convert thisJSONObjectto the specified ObjectJSONObject obj = ... User user = obj.to(User.class);- Parameters:
clazz- specify theClass<T>to be convertedfeatures- features to be enabled in parsing- Since:
- 2.0.4
-
toJavaObject
Convert thisJSONObjectto the specified Object- Parameters:
clazz- specify theClass<T>to be convertedfeatures- features to be enabled in parsing
-
toJavaObject
Deprecated.since 2.0.4, please useto(Type, JSONReader.Feature...)Convert thisJSONObjectto the specified Object- Parameters:
type- specify theTypeto be convertedfeatures- features to be enabled in parsing
-
toJavaObject
Deprecated.since 2.0.4, please useto(Type, JSONReader.Feature...)Convert thisJSONObjectto the specified Object- Parameters:
typeReference- specify theTypeReferenceto be convertedfeatures- features to be enabled in parsing
-
getObject
Returns the result of theTypeconverter conversion of the associated value in thisJSONObject.User user = jsonObject.getObject("user", User.class);- Parameters:
key- the key whose associated value is to be returnedtype- specify theClassto be converted- Returns:
<T>or null- Throws:
JSONException- If no suitable conversion method is found
-
getObject
Returns the result of theTypeconverter conversion of the associated value in thisJSONObject.User user = jsonObject.getObject("user", User.class);- Parameters:
key- the key whose associated value is to be returnedtype- specify theTypeto be convertedfeatures- features to be enabled in parsing- Returns:
<T>ornull- Throws:
JSONException- If no suitable conversion method is found
-
getObject
Returns the result of theTypeconverter conversion of the associated value in thisJSONObject.User user = jsonObject.getObject("user", User.class);- Parameters:
key- the key whose associated value is to be returnedtypeReference- specify theTypeReferenceto be convertedfeatures- features to be enabled in parsing- Returns:
<T>ornull- Throws:
JSONException- If no suitable conversion method is found- Since:
- 2.0.3
-
getObject
- Since:
- 2.0.4
-
invoke
- Specified by:
invokein interfaceInvocationHandler- Parameters:
proxy- proxy object, currently uselessmethod- methods that need reflectionargs- parameters of invoke- Throws:
UnsupportedOperationException- If reflection for this method is not supportedArrayIndexOutOfBoundsException- If the length of args does not match the length of the method parameterThrowable
-
putArray
-
putObject
-
fluentPut
Chained addition of elementsJSONObject object = new JSONObject().fluentPut("a", 1).fluentPut("b", 2).fluentPut("c", 3);- Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key
-
isValid
- Since:
- 2.0.4
-
valueFilter
- Since:
- 2.0.3
-
nameFilter
- Since:
- 2.0.3
-
clone
-
eval
- See Also:
-
getSize
if value instance of Map or Collection, return size, other return 0- Parameters:
key-- Since:
- 2.0.24
-
of
JSONObject jsonObject = JSONObject.of();
-
of
Pack a pair of key-values asJSONObjectJSONObject jsonObject = JSONObject.of("name", "fastjson2");- Parameters:
key- the key of the elementvalue- the value of the element
-
of
Pack two key-value pairs asJSONObjectJSONObject jsonObject = JSONObject.of("key1", "value1", "key2", "value2");- Parameters:
k1- first keyv1- first valuek2- second keyv2- second value- Since:
- 2.0.2
-
of
Pack three key-value pairs asJSONObjectJSONObject jsonObject = JSONObject.of("key1", "value1", "key2", "value2", "key3", "value3");- Parameters:
k1- first keyv1- first valuek2- second keyv2- second valuek3- third keyv3- third value- Since:
- 2.0.2
-
of
public static JSONObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4) Pack three key-value pairs asJSONObjectJSONObject jsonObject = JSONObject.of("key1", "value1", "key2", "value2", "key3", "value3", "key4", "value4");- Parameters:
k1- first keyv1- first valuek2- second keyv2- second valuek3- third keyv3- third valuek4- four keyv4- four value- Since:
- 2.0.8
-
of
public static JSONObject of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4, String k5, Object v5) Pack three key-value pairs asJSONObjectJSONObject jsonObject = JSONObject.of("key1", "value1", "key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5");- Parameters:
k1- first keyv1- first valuek2- second keyv2- second valuek3- third keyv3- third valuek4- four keyv4- four valuek5- five keyv5- five value- Since:
- 2.0.21
-
parseObject
SeeJSON.parseObject(java.lang.String)for details -
parseObject
SeeJSON.parseObject(java.lang.String)for details -
parseObject
SeeJSON.parseObject(java.lang.String)for details -
parseObject
public static <T> T parseObject(String text, TypeReference<T> typeReference, JSONReader.Feature... features) SeeJSON.parseObject(java.lang.String)for details -
parseObject
SeeJSON.parseObject(java.lang.String)for details -
parse
SeeJSON.parse(java.lang.String)for details- Since:
- 2.0.13
-
from
SeeJSON.toJSON(java.lang.Object)for details -
from
SeeJSON.toJSON(java.lang.Object)for details
-
to(Type, JSONReader.Feature...)