public class JSONObject extends MapWrapper<String,JSON> implements JSON, JSONGetter<String>
json = new JSONObject().put("JSON", "Hello, World!").toString();
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CAPACITY
默认初始大小
|
DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR
Constructor and Description |
---|
JSONObject()
构造,初始容量为
DEFAULT_CAPACITY ,KEY有序 |
JSONObject(int capacity)
构造
|
JSONObject(int capacity,
JSONConfig config)
构造
|
JSONObject(int capacity,
JSONFactory factory)
构造
|
JSONObject(JSONConfig config)
构造
|
Modifier and Type | Method and Description |
---|---|
JSONObject |
append(String key,
Object value)
追加值.
|
<P,T> T |
get(SerFunction<P,T> func)
根据lambda的方法引用,获取
|
JSONFactory |
getFactory()
获取JSON工厂
|
JSON |
getJSON(String key)
获取JSON对象
在JSON树模型中,JSON的节点都以JSON格式存储,所有get方法都基于此方法 |
Object |
getObj(String key,
Object defaultValue)
获取Object属性值
|
JSONObject |
increment(String key)
对值加一,如果值不存在,赋值1,如果为数字类型,做加一操作
|
JSON |
put(String key,
JSON value)
设置键值对到JSONObject中,在忽略null模式下,如果值为
null ,将此键移除 |
JSONObject |
putAllValue(Map<?,?> map)
设置所有键值对到JSONObject中,在忽略null模式下,如果值为
null ,将此键移除 |
JSONObject |
putFields(SerSupplier<?>... fields)
通过lambda批量设置值
实际使用时,可以使用getXXX的方法引用来完成键值对的赋值:
User user = GenericBuilder.of(User::new).with(User::setUsername, "hutool").build();
(new JSONObject()).setFields(user::getNickname, user::getUsername);
|
JSONObject |
putNull(String key)
设置设置
null 值。 |
JSONObject |
putValue(String key,
Boolean value)
设置键值对到JSONObject中,在忽略null模式下,如果值为
null ,将此键移除 |
JSONObject |
putValue(String key,
Character value)
设置键值对到JSONObject中,在忽略null模式下,如果值为
null ,将此键移除 |
JSONObject |
putValue(String key,
Number value)
设置键值对到JSONObject中,在忽略null模式下,如果值为
null ,将此键移除 |
JSONObject |
putValue(String key,
Object value)
设置键值对到JSONObject中,在忽略null模式下,如果值为
null ,将此键移除 |
JSONObject |
putValue(String key,
String value)
设置键值对到JSONObject中,在忽略null模式下,如果值为
null ,将此键移除 |
<K,V> Map<K,V> |
toMap(Class<K> keyType,
Class<V> valueType)
将JSONObject转换为指定类型的Map
|
String |
toString() |
void |
write(JSONWriter writer)
将JSON内容写入Writer
Warning: This method assumes that the data structure is acyclical. |
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, getRaw, hashCode, isEmpty, iterator, keySet, merge, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
finalize, getClass, notify, notifyAll, wait, wait, wait
asJSONArray, asJSONObject, asJSONPrimitive, config, getByPath, getByPath, getObjByPath, isEmpty, putByPath, size, toBean, toJSONString, toJSONString, toStringPretty
get, getBeanList, getJSONArray, getJSONObject, getStrEscaped, getStrEscaped, isNull
get, get, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBool, getBool, getByte, getByte, getBytes, getBytes, getChar, getChar, getDate, getDate, getDouble, getDouble, getEnum, getEnum, getFloat, getFloat, getInt, getInt, getLocalDate, getLocalDate, getLocalDateTime, getLocalDateTime, getLocalTime, getLocalTime, getLong, getLong, getNumber, getNumber, getObj, getShort, getShort, getSqlTime, getSqlTime, getSqlTimestamp, getSqlTimestamp, getStr, getStr
forEach, spliterator
public static final int DEFAULT_CAPACITY
public JSONObject()
DEFAULT_CAPACITY
,KEY有序public JSONObject(int capacity)
capacity
- 初始大小public JSONObject(JSONConfig config)
config
- JSON配置项public JSONObject(int capacity, JSONConfig config)
capacity
- 初始大小config
- JSON配置项,null
则使用默认配置public JSONObject(int capacity, JSONFactory factory)
capacity
- 初始大小factory
- JSON工厂类public JSONFactory getFactory()
JSON
getFactory
in interface JSON
getFactory
in interface JSONGetter<String>
public void write(JSONWriter writer) throws JSONException
JSON
write
in interface JSON
writer
- writerJSONException
- JSON相关异常public <P,T> T get(SerFunction<P,T> func)
P
- 参数类型T
- 返回值类型func
- 方法引用public Object getObj(String key, Object defaultValue)
TypeGetter
getObj
in interface TypeGetter<String>
getObj
in interface JSONGetter<String>
key
- 属性名defaultValue
- 默认值public JSON getJSON(String key)
JSONGetter
getJSON
in interface JSONGetter<String>
key
- KEYpublic JSONObject increment(String key) throws JSONException
key
- A key string.JSONException
- 如果存在值非Integer, Long, Double, 或 Float.public JSONObject append(String key, Object value) throws JSONException
key
- 键value
- 值JSONException
- 如果给定键为null
或者键对应的值存在且为非JSONArraypublic JSONObject putFields(SerSupplier<?>... fields)
User user = GenericBuilder.of(User::new).with(User::setUsername, "hutool").build();
(new JSONObject()).setFields(user::getNickname, user::getUsername);
fields
- lambda,不能为空public JSONObject putAllValue(Map<?,?> map)
null
,将此键移除map
- 键值对JSONException
- 值是无穷数字抛出此异常public JSONObject putNull(String key)
null
值。在忽略null模式下移除对应键值对。key
- 键public JSONObject putValue(String key, Boolean value) throws JSONException
null
,将此键移除key
- 键value
- Boolean类型对象JSONException
- 值是无穷数字抛出此异常public JSONObject putValue(String key, Number value) throws JSONException
null
,将此键移除key
- 键value
- Number类型对象JSONException
- 值是无穷数字抛出此异常public JSONObject putValue(String key, Character value) throws JSONException
null
,将此键移除key
- 键value
- Character类型对象JSONException
- 值是无穷数字抛出此异常public JSONObject putValue(String key, String value) throws JSONException
null
,将此键移除key
- 键value
- String类型对象JSONException
- 值是无穷数字抛出此异常public JSONObject putValue(String key, Object value) throws JSONException
null
,将此键移除key
- 键value
- 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the null
JSONException
- 值是无穷数字抛出此异常public JSON put(String key, JSON value) throws JSONException
null
,将此键移除put
in interface Map<String,JSON>
put
in class MapWrapper<String,JSON>
key
- 键value
- 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.JSONException
- 值是无穷数字抛出此异常public <K,V> Map<K,V> toMap(Class<K> keyType, Class<V> valueType)
K
- 键类型V
- 值类型keyType
- 键类型ClassvalueType
- 值类型Classpublic String toString()
toString
in class MapWrapper<String,JSON>
Copyright © 2025. All rights reserved.