Class JSONObject
- All Implemented Interfaces:
Serializable, Cloneable, Map<String,Object>, SequencedMap<String, Object>
JSON 对象,继承
LinkedHashMap 保证顺序,提供链式设置与类型化读取,参考 Hutool 的 JSONObject 设计。- Since:
- 0.1.0
- Author:
- suretool
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
ConstructorsConstructorDescription创建空 JSON 对象。JSONObject(Object bean) 从 Bean 创建 JSON 对象(不忽略 null 值)。JSONObject(Object bean, boolean ignoreNullValue) 从 Bean 创建 JSON 对象。JSONObject(String json) 解析 JSON 字符串创建 JSON 对象。JSONObject(Map<?, ?> map) 从 Map 创建 JSON 对象。 -
Method Summary
Modifier and TypeMethodDescription深拷贝(嵌套 JSONObject/JSONArray/Map/List 均递归复制)。<T> T读取并转换为 Bean。getBigDecimal(String key) 取 BigDecimal 值(金额场景)。getBigDecimal(String key, BigDecimal defaultValue) 取 BigDecimal 值,缺失或转换失败返回默认值。getBigInteger(String key) 取 BigInteger 值。getBigInteger(String key, BigInteger defaultValue) 取 BigInteger 值,缺失或转换失败返回默认值。读取布尔值,缺失返回null。boolean获取布尔值,缺失或转换失败返回默认值。byte取 byte 值。byte取 byte 值。char取 char 值。char取 char 值。取日期值(支持时间戳与常见日期字符串,转换失败返回 null)。读取浮点数,缺失或无法解析返回null。float取 float 值。float取 float 值。读取整数,缺失或无法解析返回null。getJSONArray(String key) 读取嵌套 JSON 数组。getJSONObject(String key) 读取嵌套 JSON 对象,值为 Map 时自动转换。getLocalDate(String key) 取 LocalDate 值。getLocalDateTime(String key) 取 LocalDateTime 值。读取长整数,缺失或无法解析返回null。short取 short 值。short取 short 值。读取字符串值,缺失或类型不符返回null。读取字符串值。static JSONObject解析 JSON 字符串。链式设置值。批量设置(链式)。序列化为 JSON 字符串。toString()Methods inherited from class LinkedHashMap
clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, newLinkedHashMap, putFirst, putLast, removeEldestEntry, replaceAll, reversed, sequencedEntrySet, sequencedKeySet, sequencedValues, valuesMethods inherited from class HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, newHashMap, put, putAll, putIfAbsent, remove, remove, replace, replace, sizeMethods inherited from class AbstractMap
equals, hashCodeMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, sizeMethods inherited from interface SequencedMap
firstEntry, lastEntry, pollFirstEntry, pollLastEntry
-
Constructor Details
-
JSONObject
public JSONObject()创建空 JSON 对象。 -
JSONObject
-
JSONObject
-
JSONObject
-
JSONObject
从 Bean 创建 JSON 对象。- Parameters:
bean- Bean 对象ignoreNullValue- 是否忽略null值
-
-
Method Details
-
parse
解析 JSON 字符串。- Parameters:
json- JSON 字符串- Returns:
- JSON 对象
-
set
-
getStr
-
getStr
-
getInt
-
getLong
-
getDouble
-
getBool
-
getJSONObject
读取嵌套 JSON 对象,值为 Map 时自动转换。- Parameters:
key- 键- Returns:
- JSON 对象或
null
-
getJSONArray
-
getBean
-
toJsonString
-
toString
- Overrides:
toStringin classAbstractMap<String,Object>
-
getChar
-
getChar
取 char 值。- Parameters:
key- 键defaultValue- 默认值- Returns:
- char;缺失返回默认值
-
getShort
-
getShort
取 short 值。- Parameters:
key- 键defaultValue- 默认值- Returns:
- short;缺失返回默认值
-
getByte
-
getByte
取 byte 值。- Parameters:
key- 键defaultValue- 默认值- Returns:
- byte;缺失返回默认值
-
getFloat
-
getFloat
取 float 值。- Parameters:
key- 键defaultValue- 默认值- Returns:
- float;缺失返回默认值
-
setAll
-
getDate
-
getLocalDate
-
getLocalDateTime
取 LocalDateTime 值。- Parameters:
key- 键- Returns:
- LocalDateTime;缺失或转换失败返回 null
-
getBigDecimal
取 BigDecimal 值(金额场景)。- Parameters:
key- 键- Returns:
- BigDecimal;缺失或转换失败返回 null
-
getBigDecimal
取 BigDecimal 值,缺失或转换失败返回默认值。- Parameters:
key- 键defaultValue- 默认值- Returns:
- BigDecimal 或默认值
-
getBigInteger
取 BigInteger 值。- Parameters:
key- 键- Returns:
- BigInteger;缺失或转换失败返回 null
-
getBigInteger
取 BigInteger 值,缺失或转换失败返回默认值。- Parameters:
key- 键defaultValue- 默认值- Returns:
- BigInteger 或默认值
-
deepClone
-
getBool
获取布尔值,缺失或转换失败返回默认值。- Parameters:
key- 键defaultValue- 默认值- Returns:
- 布尔值或默认值
-