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"));

    }
相关推荐
奋斗吧程序媛9 分钟前
补充一个小知识点:有关@click.native
前端·vue.js
触底反弹19 分钟前
🚀 手把手用 HTML5 Canvas 从零打造飞机大战游戏,代码全开源!
前端·javascript·canvas
飞天狗11119 分钟前
零基础JavaWeb入门——第五课第二小节:九大内置对象 · 第2个:response(响应对象)
java·开发语言
DJ斯特拉20 分钟前
axios快速使用
开发语言·前端·javascript
还有多久拿退休金23 分钟前
Ant Design Tree 搜索定位避坑指南:虚拟滚动下如何实现高亮与精准定位
前端·react.js
小月土星25 分钟前
CSS 3D 从入门到炫技:手把手教你写一个旋转立方体
前端·css
许彰午26 分钟前
39_Java单元测试JUnit入门
java·junit·单元测试
weiggle27 分钟前
第七篇:状态提升与单向数据流——架构设计的核心
android
shushangyun_27 分钟前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
JAVA96531 分钟前
JAVA面试-JVM篇 03-JVM运行时数据区哪些是线程私有的哪些是共享的
java·jvm·面试