Class JsonPath
java.lang.Object
cloud.opencode.base.json.path.JsonPath
JSON Path - XPath-style Query Language for JSON
JSON Path - JSON 的 XPath 风格查询语言
This class implements JSONPath, an XPath-like query language for JSON. It supports complex queries including wildcards, filters, and array slices.
此类实现 JSONPath,一种类似 XPath 的 JSON 查询语言。 它支持复杂查询,包括通配符、过滤器和数组切片。
Syntax | 语法:
$- Root element - 根元素.propertyor['property']- Child property - 子属性..property- Recursive descent - 递归下降*- Wildcard (all children) - 通配符(所有子元素)[n]- Array index - 数组索引[start:end]- Array slice - 数组切片[?(@.price < 10)]- Filter expression - 过滤表达式
Example | 示例:
String json = "{\"store\":{\"books\":[{\"title\":\"A\",\"price\":10},{\"title\":\"B\",\"price\":20}]}}";
JsonNode root = OpenJson.parse(json);
// Simple property access
List<JsonNode> titles = JsonPath.read(root, "$.store.books[*].title");
// Filter expression
List<JsonNode> cheap = JsonPath.read(root, "$.store.books[?(@.price < 15)]");
// Recursive descent
List<JsonNode> allPrices = JsonPath.read(root, "$..price");
Features | 主要功能:
- XPath-style query language for JSON navigation - JSON导航的XPath风格查询语言
- Wildcard, filter, array slice, and recursive descent support - 通配符、过滤器、数组切片和递归下降支持
- Compiled path expressions for reuse - 可重用的编译路径表达式
Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
- Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
- Since:
- JDK 25, opencode-base-json V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic JsonPathCompiles a JSONPath expression.Evaluates this path against a JSON node.evaluateFirst(JsonNode root) Returns the first matching node.static booleanChecks if any node matches the expression.Returns the path expression.Reads all matching values from a JSON node.static JsonNodeReads the first matching value from a JSON node.toString()
-
Method Details
-
compile
Compiles a JSONPath expression. 编译 JSONPath 表达式。- Parameters:
expression- the JSONPath expression - JSONPath 表达式- Returns:
- the compiled JsonPath - 编译的 JsonPath
- Throws:
OpenJsonProcessingException- if expression is invalid - 如果表达式无效
-
read
-
readFirst
-
exists
-
evaluate
-
evaluateFirst
-
getExpression
-
toString
-