Class JSONObject

All Implemented Interfaces:
Serializable, Cloneable, Map<String,Object>, SequencedMap<String,Object>

public class JSONObject extends LinkedHashMap<String,Object>
JSON 对象,继承 LinkedHashMap 保证顺序,提供链式设置与类型化读取,参考 Hutool 的 JSONObject 设计。
Since:
0.1.0
Author:
suretool
See Also:
  • Constructor Details

    • JSONObject

      public JSONObject()
      创建空 JSON 对象。
    • JSONObject

      public JSONObject(String json)
      解析 JSON 字符串创建 JSON 对象。
      Parameters:
      json - JSON 字符串
    • JSONObject

      public JSONObject(Map<?,?> map)
      从 Map 创建 JSON 对象。
      Parameters:
      map - Map
    • JSONObject

      public JSONObject(Object bean)
      从 Bean 创建 JSON 对象(不忽略 null 值)。
      Parameters:
      bean - Bean 对象
    • JSONObject

      public JSONObject(Object bean, boolean ignoreNullValue)
      从 Bean 创建 JSON 对象。
      Parameters:
      bean - Bean 对象
      ignoreNullValue - 是否忽略 null 值
  • Method Details

    • parse

      public static JSONObject parse(String json)
      解析 JSON 字符串。
      Parameters:
      json - JSON 字符串
      Returns:
      JSON 对象
    • set

      public JSONObject set(String key, Object value)
      链式设置值。
      Parameters:
      key - 键
      value - 值
      Returns:
      当前对象
    • getStr

      public String getStr(String key)
      读取字符串值,缺失或类型不符返回 null。
      Parameters:
      key - 键
      Returns:
      字符串值或 null
    • getStr

      public String getStr(String key, String defaultValue)
      读取字符串值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      字符串值或默认值
    • getInt

      public Integer getInt(String key)
      读取整数,缺失或无法解析返回 null。
      Parameters:
      key - 键
      Returns:
      整数值或 null
    • getLong

      public Long getLong(String key)
      读取长整数,缺失或无法解析返回 null。
      Parameters:
      key - 键
      Returns:
      长整数值或 null
    • getDouble

      public Double getDouble(String key)
      读取浮点数,缺失或无法解析返回 null。
      Parameters:
      key - 键
      Returns:
      浮点值或 null
    • getBool

      public Boolean getBool(String key)
      读取布尔值,缺失返回 null。
      Parameters:
      key - 键
      Returns:
      布尔值或 null
    • getJSONObject

      public JSONObject getJSONObject(String key)
      读取嵌套 JSON 对象,值为 Map 时自动转换。
      Parameters:
      key - 键
      Returns:
      JSON 对象或 null
    • getJSONArray

      public JSONArray getJSONArray(String key)
      读取嵌套 JSON 数组。
      Parameters:
      key - 键
      Returns:
      JSON 数组或 null
    • getBean

      public <T> T getBean(String key, Class<T> beanClass)
      读取并转换为 Bean。
      Type Parameters:
      T - Bean 类型
      Parameters:
      key - 键
      beanClass - Bean 类
      Returns:
      Bean 或 null
    • toJsonString

      public String toJsonString()
      序列化为 JSON 字符串。
      Returns:
      JSON 字符串
    • toString

      public String toString()
      Overrides:
      toString in class AbstractMap<String,Object>
    • getChar

      public char getChar(String key)
      取 char 值。
      Parameters:
      key - 键
      Returns:
      char;缺失返回 0
    • getChar

      public char getChar(String key, char defaultValue)
      取 char 值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      char;缺失返回默认值
    • getShort

      public short getShort(String key)
      取 short 值。
      Parameters:
      key - 键
      Returns:
      short;缺失返回 0
    • getShort

      public short getShort(String key, short defaultValue)
      取 short 值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      short;缺失返回默认值
    • getByte

      public byte getByte(String key)
      取 byte 值。
      Parameters:
      key - 键
      Returns:
      byte;缺失返回 0
    • getByte

      public byte getByte(String key, byte defaultValue)
      取 byte 值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      byte;缺失返回默认值
    • getFloat

      public float getFloat(String key)
      取 float 值。
      Parameters:
      key - 键
      Returns:
      float;缺失返回 0
    • getFloat

      public float getFloat(String key, float defaultValue)
      取 float 值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      float;缺失返回默认值
    • setAll

      public JSONObject setAll(Map<?,?> map)
      批量设置(链式)。
      Parameters:
      map - 键值映射
      Returns:
      this
    • getDate

      public Date getDate(String key)
      取日期值(支持时间戳与常见日期字符串,转换失败返回 null)。
      Parameters:
      key - 键
      Returns:
      日期;缺失或转换失败返回 null
    • getLocalDate

      public LocalDate getLocalDate(String key)
      取 LocalDate 值。
      Parameters:
      key - 键
      Returns:
      LocalDate;缺失或转换失败返回 null
    • getLocalDateTime

      public LocalDateTime getLocalDateTime(String key)
      取 LocalDateTime 值。
      Parameters:
      key - 键
      Returns:
      LocalDateTime;缺失或转换失败返回 null
    • getBigDecimal

      public BigDecimal getBigDecimal(String key)
      取 BigDecimal 值(金额场景)。
      Parameters:
      key - 键
      Returns:
      BigDecimal;缺失或转换失败返回 null
    • getBigDecimal

      public BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
      取 BigDecimal 值,缺失或转换失败返回默认值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      BigDecimal 或默认值
    • getBigInteger

      public BigInteger getBigInteger(String key)
      取 BigInteger 值。
      Parameters:
      key - 键
      Returns:
      BigInteger;缺失或转换失败返回 null
    • getBigInteger

      public BigInteger getBigInteger(String key, BigInteger defaultValue)
      取 BigInteger 值,缺失或转换失败返回默认值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      BigInteger 或默认值
    • deepClone

      public JSONObject deepClone()
      深拷贝(嵌套 JSONObject/JSONArray/Map/List 均递归复制)。
      Returns:
      深拷贝副本
    • getBool

      public boolean getBool(String key, boolean defaultValue)
      获取布尔值,缺失或转换失败返回默认值。
      Parameters:
      key - 键
      defaultValue - 默认值
      Returns:
      布尔值或默认值