Class JSONArray

All Implemented Interfaces:
Serializable, Cloneable, Iterable<Object>, Collection<Object>, List<Object>, RandomAccess, SequencedCollection<Object>

public class JSONArray extends ArrayList<Object>
JSON 数组,继承 ArrayList,提供类型化读取,参考 Hutool 的 JSONArray 设计。
Since:
0.1.0
Author:
suretool
See Also:
  • Constructor Details

    • JSONArray

      public JSONArray()
      创建空 JSON 数组。
    • JSONArray

      public JSONArray(String json)
      解析 JSON 字符串创建 JSON 数组。
      Parameters:
      json - JSON 字符串
    • JSONArray

      public JSONArray(Collection<?> collection)
      从集合创建 JSON 数组。
      Parameters:
      collection - 集合
    • JSONArray

      public JSONArray(Object... items)
      从可变参数创建 JSON 数组。
      Parameters:
      items - 元素
  • Method Details

    • parse

      public static JSONArray parse(String json)
      解析 JSON 字符串。
      Parameters:
      json - JSON 字符串
      Returns:
      JSON 数组
    • getStr

      public String getStr(int index)
      读取字符串值,越界或为 null 返回 null。
      Parameters:
      index - 索引
      Returns:
      字符串值或 null
    • getInt

      public Integer getInt(int index)
      读取整数,越界或无法解析返回 null。
      Parameters:
      index - 索引
      Returns:
      整数值或 null
    • getLong

      public Long getLong(int index)
      读取长整数,越界或无法解析返回 null。
      Parameters:
      index - 索引
      Returns:
      长整数值或 null
    • getDouble

      public Double getDouble(int index)
      读取浮点数,越界或无法解析返回 null。
      Parameters:
      index - 索引
      Returns:
      浮点值或 null
    • getBool

      public Boolean getBool(int index)
      读取布尔值,越界返回 null。
      Parameters:
      index - 索引
      Returns:
      布尔值或 null
    • getJSONObject

      public JSONObject getJSONObject(int index)
      读取嵌套 JSON 对象。
      Parameters:
      index - 索引
      Returns:
      JSON 对象或 null
    • getJSONArray

      public JSONArray getJSONArray(int index)
      读取嵌套 JSON 数组。
      Parameters:
      index - 索引
      Returns:
      JSON 数组或 null
    • getBean

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

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

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

      public char getChar(int index)
      取 char 值。
      Parameters:
      index - 下标
      Returns:
      char;缺失返回 0
    • getChar

      public char getChar(int index, char defaultValue)
      取 char 值。
      Parameters:
      index - 下标
      defaultValue - 默认值
      Returns:
      char;缺失返回默认值
    • getShort

      public short getShort(int index)
      取 short 值。
      Parameters:
      index - 下标
      Returns:
      short;缺失返回 0
    • getShort

      public short getShort(int index, short defaultValue)
      取 short 值。
      Parameters:
      index - 下标
      defaultValue - 默认值
      Returns:
      short;缺失返回默认值
    • getByte

      public byte getByte(int index)
      取 byte 值。
      Parameters:
      index - 下标
      Returns:
      byte;缺失返回 0
    • getByte

      public byte getByte(int index, byte defaultValue)
      取 byte 值。
      Parameters:
      index - 下标
      defaultValue - 默认值
      Returns:
      byte;缺失返回默认值
    • getFloat

      public float getFloat(int index)
      取 float 值。
      Parameters:
      index - 下标
      Returns:
      float;缺失返回 0
    • getFloat

      public float getFloat(int index, float defaultValue)
      取 float 值。
      Parameters:
      index - 下标
      defaultValue - 默认值
      Returns:
      float;缺失返回默认值
    • getDate

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

      public LocalDate getLocalDate(int index)
      取 LocalDate 值。
      Parameters:
      index - 下标
      Returns:
      LocalDate;越界或转换失败返回 null
    • getLocalDateTime

      public LocalDateTime getLocalDateTime(int index)
      取 LocalDateTime 值。
      Parameters:
      index - 下标
      Returns:
      LocalDateTime;越界或转换失败返回 null
    • getBigDecimal

      public BigDecimal getBigDecimal(int index)
      取 BigDecimal 值(金额场景)。
      Parameters:
      index - 下标
      Returns:
      BigDecimal;缺失或转换失败返回 null
    • getBigDecimal

      public BigDecimal getBigDecimal(int index, BigDecimal defaultValue)
      取 BigDecimal 值,越界或转换失败返回默认值。
      Parameters:
      index - 下标
      defaultValue - 默认值
      Returns:
      BigDecimal 或默认值
    • getBigInteger

      public BigInteger getBigInteger(int index)
      取 BigInteger 值。
      Parameters:
      index - 下标
      Returns:
      BigInteger;越界或转换失败返回 null
    • getBigInteger

      public BigInteger getBigInteger(int index, BigInteger defaultValue)
      取 BigInteger 值,越界或转换失败返回默认值。
      Parameters:
      index - 下标
      defaultValue - 默认值
      Returns:
      BigInteger 或默认值
    • toList

      public <T> List<T> toList(Class<T> beanClass)
      转为对象列表(元素为 JSONObject 时按 beanClass 转换)。
      Type Parameters:
      T - 泛型
      Parameters:
      beanClass - 目标类型
      Returns:
      对象列表
    • toArray

      public <T> T[] toArray(Class<T> beanClass)
      转为对象数组(元素为 JSONObject 时按 beanClass 转换)。
      Type Parameters:
      T - 泛型
      Parameters:
      beanClass - 目标类型
      Returns:
      对象数组
    • deepClone

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

      public String join(String delimiter)
      将数组元素用分隔符拼接。
      Parameters:
      delimiter - 分隔符
      Returns:
      拼接字符串