Class JsonPathUtil

java.lang.Object
com.ultikits.ultitools.utils.JsonPathUtil

public final class JsonPathUtil extends Object
JSON 路径工具类 - 替代 hutool-json 的 putByPath/getByPath 功能

提供通过点分隔的路径访问和设置嵌套 JSON Map 中的值。

 使用示例:
 Map<String, Object> json = new LinkedHashMap<>();
 JsonPathUtil.putByPath(json, "a.b.c", "value");
 Object value = JsonPathUtil.getByPath(json, "a.b.c"); // "value"
 
Since:
6.2.0
Author:
UltiKits Team
  • Constructor Details

    • JsonPathUtil

      private JsonPathUtil()
  • Method Details

    • getByPath

      public static Object getByPath(Map<String,Object> json, String path)
      通过路径获取嵌套 Map 中的值
      Parameters:
      json - JSON Map 对象
      path - 点分隔的路径,如 "a.b.c"
      Returns:
      路径对应的值,如果路径不存在或中间节点非 Map 则返回 null
    • getStr

      public static String getStr(Map<String,Object> json, String path)
      通过路径获取嵌套 Map 中的 String 值
      Parameters:
      json - JSON Map 对象
      path - 点分隔的路径
      Returns:
      String 值,如果值不存在或不是 String 则返回 null
    • getInt

      public static Integer getInt(Map<String,Object> json, String path)
      通过路径获取嵌套 Map 中的 Integer 值
      Parameters:
      json - JSON Map 对象
      path - 点分隔的路径
      Returns:
      Integer 值,如果值不存在或无法转换则返回 null
    • getLong

      public static Long getLong(Map<String,Object> json, String path)
      通过路径获取嵌套 Map 中的 Long 值
      Parameters:
      json - JSON Map 对象
      path - 点分隔的路径
      Returns:
      Long 值,如果值不存在或无法转换则返回 null
    • getBool

      public static Boolean getBool(Map<String,Object> json, String path)
      通过路径获取嵌套 Map 中的 Boolean 值
      Parameters:
      json - JSON Map 对象
      path - 点分隔的路径
      Returns:
      Boolean 值,如果值不存在则返回 null
    • getDouble

      public static Double getDouble(Map<String,Object> json, String path)
      通过路径获取嵌套 Map 中的 Double 值
      Parameters:
      json - JSON Map 对象
      path - 点分隔的路径
      Returns:
      Double 值,如果值不存在或无法转换则返回 null
    • putByPath

      public static void putByPath(Map<String,Object> json, String path, Object value)
      通过路径设置嵌套 Map 中的值,自动创建中间层级
      Parameters:
      json - JSON Map 对象
      path - 点分隔的路径,如 "a.b.c"
      value - 要设置的值
    • removeByPath

      public static Object removeByPath(Map<String,Object> json, String path)
      通过路径删除嵌套 Map 中的值
      Parameters:
      json - JSON Map 对象
      path - 点分隔的路径
      Returns:
      被删除的值,如果路径不存在返回 null
    • containsPath

      public static boolean containsPath(Map<String,Object> json, String path)
      检查路径是否存在
      Parameters:
      json - JSON Map 对象
      path - 点分隔的路径
      Returns:
      如果路径存在且值不为 null 返回 true