使用 Fastjson 的 JSONPath.eval 可以通过 JSONPath 表达式直接定位多层级 JSON 中的目标字段,避免逐层调用 getJSONObject() 的繁琐操作。以下是具体实现方法和示例:
核心思路
通过 JSONPath.eval 方法,传入 JSON 对象(或 JSON 字符串)和 JSONPath 表达式,直接提取目标字段的值。JSONPath 表达式类似 XPath,用简洁的路径语法描述嵌套结构(例如 $.user.info.contact.email 表示根节点下 user 对象的 info 对象的 contact 对象的 email 字段)。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;public class JsonPathEvalDemo {public static void main(String[] args) {// 示例多层级 JSON 数据(与之前保持一致)String jsonStr = "{\n" +"  \"user\": {\n" +"    \"info\": {\n" +"      \"name\": \"张三\",\n" +"      \"age\": 25,\n" +"      \"contact\": {\n" +"        \"email\": \"zhangsan@example.com\",\n" +"        \"phone\": \"138-1234-5678\"\n" +"      }\n" +"    },\n" +"    \"login\": {\n" +"      \"last_login_time\": \"2025-05-05 20:30:00\",\n" +"      \"is_valid\": true\n" +"    }\n" +"  }\n" +"}";try {// 1. 解析 JSON 字符串为 JSONObjectJSONObject root = JSON.parseObject(jsonStr);// 2. 逐层获取嵌套对象JSONObject user = root.getJSONObject("user");         // 获取 "user" 对象JSONObject info = user.getJSONObject("info");         // 获取 "info" 对象JSONObject contact = info.getJSONObject("contact");   // 获取 "contact" 对象// 3. 获取目标字段值(支持多种类型)String name = info.getString("name");         // 姓名(字符串)int age = info.getIntValue("age");            // 年龄(整数)String email = contact.getString("email");    // 邮箱(字符串)boolean isValid = user.getJSONObject("login").getBooleanValue("is_valid");  // 登录有效性(布尔值)// 输出结果System.out.println("姓名: " + name);System.out.println("年龄: " + age);System.out.println("邮箱: " + email);System.out.println("登录有效性: " + isValid);} catch (Exception e) {e.printStackTrace();System.out.println("解析 JSON 失败: " + e.getMessage());}// 解析 JSON 为 JSONObject(或直接对 JSON 字符串使用 JSONPath)JSONObject root = JSON.parseObject(jsonStr);try {// 1. 用 JSONPath 表达式直接获取目标字段String name = (String) JSONPath.eval(root, "$.user.info.name");          // 姓名Integer age = (Integer) JSONPath.eval(root, "$.user.info.age");          // 年龄String email = (String) JSONPath.eval(root, "$.user.info.contact.email");// 邮箱Boolean isValid = (Boolean) JSONPath.eval(root, "$.user.login.is_valid");// 登录有效性// 2. 输出结果System.out.println("姓名: " + name);System.out.println("年龄: " + age);System.out.println("邮箱: " + email);System.out.println("登录有效性: " + isValid);// 3. 扩展:直接对 JSON 字符串使用(无需先解析为 JSONObject)String lastLoginTime = (String) JSONPath.eval(jsonStr, "$.user.login.last_login_time");System.out.println("最后登录时间: " + lastLoginTime);} catch (Exception e) {e.printStackTrace();System.out.println("JSONPath 解析失败: " + e.getMessage());}}
} 
 
1. JSONPath 表达式语法
$:表示根节点(类似 XPath 的/)。.或[]:访问子节点(例如$.user或$['user']效果相同)。- 嵌套层级:用连续的 
.拼接路径(例如$.user.info.contact.email)。 - 数组支持:若字段是数组,可用 
[index]定位元素(例如$.users[0].name表示第一个用户的姓名)。 
2.字段不存在处理
若路径不存在,JSONPath.eval 会返回 null(不会抛异常),需注意判空:
String email = (String) JSONPath.eval(root, "$.user.info.contact.email");
if (email == null) {System.out.println("邮箱字段不存在");
} 
高级用法(可选)
- 通配符 
*:匹配所有子节点(例如$.user.info.*获取info下所有字段)。 - 过滤表达式:用 
?()筛选符合条件的元素(例如$.users[?(@.age > 20)]筛选年龄大于 20 的用户)。 - 递归匹配 
..:递归查找所有层级的目标字段(例如$..email匹配所有层级的email字段)。 
通过 JSONPath.eval,可以更简洁、灵活地处理多层级 JSON 数据,尤其适合嵌套深、结构复杂的场景。