Fastjson 从多层级的JSON数据中获取特定字段的值

使用 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 字符串为 JSONObject
            JSONObject 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 数据,尤其适合嵌套深、结构复杂的场景。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值