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

常用语法

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

过滤表达式

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

范围表达式

表达式 说明
$.array[start:end] 选择根元素中从start索引到end索引之间的数组元素
$.array[:end] 选择根元素中从开头到end索引之间的数组元素
$.array[start:] 选择根元素中从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"));

    }
相关推荐
枷锁—sha1 分钟前
跨站请求伪造漏洞(CSRF)详解
运维·服务器·前端·web安全·网络安全·csrf
ldj202013 分钟前
SpringBoot为什么使用new RuntimeException() 来获取调用栈?
java·spring boot·后端
超龄超能程序猿14 分钟前
Spring 应用中 Swagger 2.0 迁移 OpenAPI 3.0 详解:配置、注解与实践
java·spring boot·后端·spring·spring cloud
群联云防护小杜17 分钟前
深度隐匿源IP:高防+群联AI云防护防绕过实战
运维·服务器·前端·网络·人工智能·网络协议·tcp/ip
风象南26 分钟前
SpringBoot配置属性热更新的轻量级实现
java·spring boot·后端
洛阳泰山27 分钟前
Spring Boot 整合 Nacos 实战教程:服务注册发现与配置中心详解
java·spring boot·后端·nacos
Y40900127 分钟前
C语言转Java语言,相同与相异之处
java·c语言·开发语言·笔记
YuTaoShao28 分钟前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
布朗克16828 分钟前
java常见的jvm内存分析工具
java·jvm·数据库
汉得数字平台35 分钟前
【鲲苍提效】全面洞察用户体验,助力打造高性能前端应用
前端·前端监控