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

    }
相关推荐
wuyk5551 小时前
AI 时代的嵌入式该怎么学-19-MCU上为什么禁用malloc
java·服务器·人工智能·stm32·单片机·mcu·物联网
Nazarick1 小时前
Java Web 数据库开发实战:从传统连接到 DAO 封装与前后端交互
java·数据库·mysql·web
Mh7 小时前
Three.js 实现 3D 汽车交互2.0版本
前端·three.js
我要见SA姐18 小时前
用生活化类比当钥匙,一键打开 C++ 入门大门
java·开发语言·c++
2601_958352908 小时前
I2S转模拟音频,真的只要5个引脚+1个电容?
前端·人工智能·单片机·嵌入式硬件
LaughingZhu9 小时前
Product Hunt 热榜 2026-12-05:SoduWork、finerd split、Otari by Mozilla AI
java·人工智能·docker·chatgpt·容器
好的佩奇9 小时前
Dart 之测试
android·flutter·dart
好的佩奇10 小时前
蓝牙之 GATT 数据操作
android·flutter·蓝牙
L-影10 小时前
HashMap详解-上篇
java·开发语言·spring·hashmap
爱笑的源码基地10 小时前
什么是智能预问诊系统?一套java版预问诊成品源码
java·源码·程序·预问诊·智能问诊·预问诊源码