Package com.ultikits.ultitools.utils
Class JsonPathUtil
java.lang.Object
com.ultikits.ultitools.utils.JsonPathUtil
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontainsPath(Map<String, Object> json, String path) 检查路径是否存在static Boolean通过路径获取嵌套 Map 中的 Boolean 值static Object通过路径获取嵌套 Map 中的值static Double通过路径获取嵌套 Map 中的 Double 值static Integer通过路径获取嵌套 Map 中的 Integer 值static Long通过路径获取嵌套 Map 中的 Long 值static String通过路径获取嵌套 Map 中的 String 值static void通过路径设置嵌套 Map 中的值,自动创建中间层级static ObjectremoveByPath(Map<String, Object> json, String path) 通过路径删除嵌套 Map 中的值
-
Constructor Details
-
JsonPathUtil
private JsonPathUtil()
-
-
Method Details
-
getByPath
通过路径获取嵌套 Map 中的值- Parameters:
json- JSON Map 对象path- 点分隔的路径,如 "a.b.c"- Returns:
- 路径对应的值,如果路径不存在或中间节点非 Map 则返回 null
-
getStr
通过路径获取嵌套 Map 中的 String 值- Parameters:
json- JSON Map 对象path- 点分隔的路径- Returns:
- String 值,如果值不存在或不是 String 则返回 null
-
getInt
通过路径获取嵌套 Map 中的 Integer 值- Parameters:
json- JSON Map 对象path- 点分隔的路径- Returns:
- Integer 值,如果值不存在或无法转换则返回 null
-
getLong
通过路径获取嵌套 Map 中的 Long 值- Parameters:
json- JSON Map 对象path- 点分隔的路径- Returns:
- Long 值,如果值不存在或无法转换则返回 null
-
getBool
通过路径获取嵌套 Map 中的 Boolean 值- Parameters:
json- JSON Map 对象path- 点分隔的路径- Returns:
- Boolean 值,如果值不存在则返回 null
-
getDouble
通过路径获取嵌套 Map 中的 Double 值- Parameters:
json- JSON Map 对象path- 点分隔的路径- Returns:
- Double 值,如果值不存在或无法转换则返回 null
-
putByPath
通过路径设置嵌套 Map 中的值,自动创建中间层级- Parameters:
json- JSON Map 对象path- 点分隔的路径,如 "a.b.c"value- 要设置的值
-
removeByPath
通过路径删除嵌套 Map 中的值- Parameters:
json- JSON Map 对象path- 点分隔的路径- Returns:
- 被删除的值,如果路径不存在返回 null
-
containsPath
检查路径是否存在- Parameters:
json- JSON Map 对象path- 点分隔的路径- Returns:
- 如果路径存在且值不为 null 返回 true
-