JsonPath 数据快速查找和提取工具

常用语法

表达式 说明
$ 表示根元素
$.key 选择根元素下的指定键名的值
$.* 选择根元素下的所有属性值
$.array\* 选择根元素中的数组的所有元素
$.keysubkey 选择根元素中的键名为key,子键名为subkey的值
$.key\*.subkey 选择根元素中的键名为key的所有元素的子键名为subkey的值

过滤表达式

表达式 说明
$.key?(@.subkey == value) 选择根元素中key为指定值且具有subkey并且值等于value的元素
$.array?(@.value \> 10) 选择根元素中值大于10的数组元素

范围表达式

表达式 说明
$.arraystart:end 选择根元素中从start索引到end索引之间的数组元素
$.array:end 选择根元素中从开头到end索引之间的数组元素
$.arraystart: 选择根元素中从start索引到末尾的数组元素

通配符表达式

表达式 说明
$.* 选择根元素下的所有键值对
$...key 选择根元素和所有子元素中的具有指定键名的值

操作符表达式

表达式 说明
$.key?(@.value \> 10 \&\& @.value \< 20) 选择根元素中值大于10且小于20的key
$.key?(@.name =\~ /pattern/) 选择根元素中name符合正则表达式pattern的key

操作实战

json数据

json 复制代码
{
	"store": {
		"book": [{
			"category": "reference",
			"author": "Nigel Rees",
			"title": "Sayings of the Century",
			"price": 8.95
		}, {
			"category": "fiction",
			"author": "Evelyn Waugh",
			"title": "Sword of Honour",
			"price": 12.99,
			"isbn": "0-553-21311-3"
		}],
		"bicycle": {
			"color": "red",
			"price": 19.95
		}
	}
}

引入依赖

xml 复制代码
 		<dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.40</version>
        </dependency>

示例

java 复制代码
public static void main(String[] args) {
        String jsonStr = "{ \"store\": {\"book\": [{ \"category\": \"reference\"," +
                "\"author\": \"Nigel Rees\",\"title\": \"Sayings of the Century\"," +
                "\"price\": 8.95},{ \"category\": \"fiction\",\"author\": \"Evelyn Waugh\"," +
                "\"title\": \"Sword of Honour\",\"price\": 12.99,\"isbn\": \"0-553-21311-3\"" +
                "}],\"bicycle\": {\"color\": \"red\",\"price\": 19.95}}}";
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        System.out.println(jsonStr);

        System.out.println("book数目:"+ JSONPath.eval(jsonObject, "$.store.book.size()") );
        System.out.println("第一本书的title:"+JSONPath.eval(jsonObject,"$.store.book[0].title"));
        System.out.println("第一本书的category和author:"+JSONPath.eval(jsonObject,"$.store.book[0]['category','author']"));
        System.out.println("price>10的书:"+JSONPath.eval(jsonObject,"$.store.book[?(@.price > 10)]"));
        System.out.println("price>8的书的标题:"+JSONPath.eval(jsonObject,"$.store.book[?(@.price > 8)]"));
        System.out.println("price>7的书:"+JSONPath.eval(jsonObject,"$.store.book[?(@.price > 7)]"));
        System.out.println("price>7的书的标题:"+JSONPath.eval(jsonObject,"$.store.book[?(@.price > 7)].title"));
        System.out.println("书的标题为Sayings of the Century:"+JSONPath.eval(jsonObject,"$.store.book[?(@.title='Sayings of the Century')]"));
        System.out.println("bicycle的所有属性:"+JSONPath.eval(jsonObject,"$.store.bicycle.*"));
        System.out.println("bicycle:"+JSONPath.eval(jsonObject,"$.store.bicycle"));
        System.out.println("所有price:"+JSONPath.eval(jsonObject,"$.store.book[*].price"));

    }
相关推荐
哲鲤几秒前
Binder 不只是 IPC:从一次跨进程调用看懂 Android 与 AAOS 的架构演进
android
宝哥聊编程5 分钟前
主流 Agent 平台对比:Coze、Dify、飞书、扣子、OpenClaw 落地实操手册
前端·人工智能·飞书
用户128139778167 分钟前
Eyf半字节【3.0 版】正式发布|订单、单证、扫码三大核心模块全新重构
android
云析赢指标公式网422 分钟前
文华财经指标源码笔记:WINNER筹码函数、控盘变量与MACD变体的调试记录
java·笔记·算法
时间的情敌27 分钟前
Vue3 + Cesium 实战:本地上传 GLB 模型、模型上叠加 GLB、点击控制透明度
前端·vue.js·cesium
拖孩31 分钟前
流量主门槛从 500 降到 100,我的小程序上线 18 小时够到了线
前端·后端·微信小程序
从琳开始98939 分钟前
从Socket到TCP回显服务器:Java网络编程完整指南
java·服务器·网络·tcp/ip·java-ee
养军博客39 分钟前
Spring Security 4.x / 5.x / 6.x 配置全对比:前后端不分离与前后端分离
java·后端·spring·springsecurity
深蓝AI40 分钟前
编码 Agent 的界面味靠提示词压不住:Impeccable 用无模型规则拦同一套模板
前端·程序员
wifi___40 分钟前
SpringBoot自动装配原理
java·spring boot·后端