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

编程入门 行业动态 更新时间:2024-10-21 14:34:56

JsonPath 数据<a href=https://www.elefans.com/category/jswz/34/1762955.html style=快速查找和提取工具"/>

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数据

{"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}}
}

引入依赖

 		<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.40</version></dependency>

示例

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

更多推荐

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

本文发布于:2023-11-16 21:53:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633097.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快速查找   工具   数据   JsonPath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!