文档章节

JSONPath入门之Snack3篇

刘之西东
 刘之西东
发布于 2019/11/27 16:07
字数 1398
阅读 28
收藏 0

Snack3 for java

一个微型JSON框架

基于jdk8,60kb。有序列化反序列化、解析和转换、支持 Json path 查询。

<dependency>
  <groupId>org.noear</groupId>
  <artifactId>snack3</artifactId>
  <version>3.1.5.9</version>
</dependency>

Snack3 借签了 Javascript 所有变量由 var 申明,及 Xml dom 一切都是 Node 的设计。其下一切数据都以ONode表示,ONode也即 One node 之意,代表任何类型,也可以转换为任何类型。

  • 强调文档树的操控和构建能力
  • 做为中间媒体,方便不同格式互转
  • 高性能Json path查询(兼容性和性能很赞)
  • 支持序列化、反序列化

今天用它来试一下JSONPath入门

一、JSONPath语法介绍

  • 字符串使用单引号,例:['name']
  • 过滤操作用空隔号隔开,例:[?(@.type == 1)]
支持操作 说明
$ 表示根元素
@ 当前节点(做为过滤表达式的谓词使用)
* 通用配配符,可以表示一个名字或数字。
.. 深层扫描。 可以理解为递归搜索。
.<name> 表示一个子节点
['<name>' (, '<name>')] 表示一个或多个子节点
[<number> (, <number>)] 表示一个或多个数组下标(负号为倒数)
[start:end] 数组片段,区间为[start,end),不包含end(负号为倒数)
[?(<expression>)] 过滤表达式。 表达式结果必须是一个布尔值。
支持过滤操作符 说明
== left等于right(注意1不等于'1')
!= 不等于
< 小于
<= 小于等于
> 大于
>= 大于等于
=~ 匹配正则表达式[?(@.name =~ /foo.*?/i)]
in 左边存在于右边 [?(@.size in ['S', 'M'])]
nin 左边不存在于右边
支持尾部函数 说明
min() 计算数字数组的最小值
max() 计算数字数组的最大值
avg() 计算数字数组的平均值
sum() 计算数字数组的汇总值(新加的)

 二、准备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
		}, {
			"category": "fiction",
			"author": "Herman Melville",
			"title": "Moby Dick",
			"isbn": "0-553-21311-3",
			"price": 8.99
		}, {
			"category": "fiction",
			"author": "J. R. R. Tolkien",
			"title": "The Lord of the Rings",
			"isbn": "0-395-19395-8",
			"price": 22.99
		}],
		"bicycle": {
			"color": "red",
			"price": 19.95
		}
	},
	"expensive": 10
}

三、演示代码

@Test
public void demo1(){
    final String 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},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";

    ONode n = ONode.load(json);

    ONode t1 = n.select("$.store.book [0].title");
    System.out.println("\nt1:" + t1);

    ONode t2 = n.select("$['store']['book'][0]['title']");
    System.out.println("\nt2:" + t2);

    ONode t3 = n.select("$.store.book[*].author");
    System.out.println("\nt3:" + t3);

    ONode t4 = n.select("$..author");
    System.out.println("\nt4:" + t4);

    ONode t5 = n.select("$.store.*");
    System.out.println("\nt5:" + t5);

    ONode t6 = n.select("$.store..price");
    System.out.println("\nt6:" + t6);

    ONode t7 = n.select("$..book[2]");
    System.out.println("\nt7:" + t7);

    ONode t8 =  n.select("$..book[-2]");
    System.out.println("\nt8:" + t8);

    ONode t9 = n.select("$..book[0,1]");
    System.out.println("\nt9:" + t9);

    ONode ta = n.select("$..book[:2]");
    System.out.println("\nta:" + ta);

    ONode tb = n.select("$..book[1:2]");
    System.out.println("\ntb:" + tb);

    ONode tc = n.select("$..book[-2:]");
    System.out.println("\ntc:" + tc);

    ONode td = n.select("$..book[2:]");
    System.out.println("\ntd:" + td);

    ONode te = n.select("$..book[?(@.isbn)]");
    System.out.println("\nte:" + te);

    ONode tf = n.select("$.store.book[?(@.price < 10)]");
    System.out.println("\ntf:" + tf);

    ONode tg = n.select("$..book[?(@.author =~ /.*REES/i)]");
    System.out.println("\ntg:" + tg);

    ONode th = n.select("$..*");
    System.out.println("\nth:" + th);

    ONode ti = n.select("$..book[?(@.price <= $.expensive)]");
    System.out.println("\nti:" + ti);
}

四、控制台输出

t1:"Sayings of the Century"

t2:"Sayings of the Century"

t3:["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]

t4:["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]

t5:[[{"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},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95}]

t6:[8.95,12.99,8.99,22.99,19.95]

t7:[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]

t8:[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]

t9:[{"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}]

ta:[{"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}]

tb:[{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}]

tc:[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]

td:[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]

te:[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}]

tf:[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]

tg:[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95}]

th:[{"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},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}},[{"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},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},"reference","Nigel Rees","Sayings of the Century",8.95,{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},"fiction","Evelyn Waugh","Sword of Honour",12.99,{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},"fiction","Herman Melville","Moby Dick","0-553-21311-3",8.99,{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99},"fiction","J. R. R. Tolkien","The Lord of the Rings","0-395-19395-8",22.99,{"color":"red","price":19.95},"red",19.95,10]

ti:[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]

© 著作权归作者所有

刘之西东

刘之西东

粉丝 3
博文 28
码字总数 27523
作品 3
杭州
CTO(技术副总裁)
私信 提问
加载中

评论(0)

接口测试框架优化(三)---支持简单串接口

前言 之前两篇文章,第一篇介绍了主要结构,第二篇介绍了主要代码,最近做了一点小优化。 先贴Github地址,本次主要优化一点: 支持单个简单串接口的测试,即下一个接口的一个参数依赖上一个接...

迈阿密小白
2018/05/11
0
0
ModuleNotFoundError: No module named 'jsonpath'

刚才在运行时发现没有这个模块而控制台报错 :NameError: name 'jsonpath' is not defined 想着name 'jsonpath'是不是没有安装,然后就在cmd窗口pip install jsonpath 果然 安装完成后pycha......

anlve
2018/01/12
1.2K
0
spring 3.2.0ga 测试框架

spring3.2终于出ga版了, 发现spring test的改进最大,添加了对 springmvc的测试 看了看文档,将使用方法整理如下: 1,可以使用 @WebAppConfiguration来标明是web应用测试, @ContextConfig...

big-hero
2012/12/20
2K
1
fastjson 1.2.52 版本发布,Java 实现的 JSON 解析库

fastjson 1.2.52 版本发布了,这又是一个功能增强,以及 bug 修复的版本。主要是增强对 JSONPath 的支持。 Issues 增强JSONPath的支持,增强新的JSONPath.extract API,根据path按需解析JSO...

局长
2018/11/19
2.1K
19
后端服务的查询语言和执行引擎 - GraphQuery

GraphQuery —— 与任何后端服务相关联的查询语言和执行引擎 概述 GraphQuery 是一门易于使用的查询语言,它内置了 Xpath/CSS/Regex/JSONpath 选择器和文本处理功能。 你可以使用简洁的 Grap...

故事细腻_
2018/10/26
792
0

没有更多内容

加载失败,请刷新页面

加载更多

Spring基础

主要用于service层; 轻量级java开发框架; 各层 web层:struts,spring-MVC service层:spring dao层:hibernate,mybatis , jdbcTemplate --> spring-data Spring核心:控制反转IOC 切面编...

七宝1
14分钟前
19
0
解决overflow+border-radius+transform圆角问题

网上还有其他版本,但是对我来说都不好使,下面是我在Chrome上的代码。overflow:hidden依然是不能正常使用,换成unset就可以,读者如果有更好的解决方案,请留言,谢谢。 <figure> <img...

hi懒喵
20分钟前
39
0
《C语言》—— 数组

书籍使我变成了一个幸福的人,使我的生活变成轻松而舒适的诗。——高尔基 本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues。 https://github.com/midou-tech/articles 点关注,不迷路!...

龙跃十二
33分钟前
60
0
Java小案例: 1000以内的水仙花数

要求在控制台分别展示1000以内的水仙花数。 如下:↓ 水仙花数规则: 水仙花数至少是一个3位数 每位数字的立方数的累加和,正好等于该数字本身。 例如:153就是一个水仙花数。每位数字分别...

漫路h
昨天
66
0
centos 防火墙端口设置

查看开放的端口号 firewall-cmd --lists-all 设置开放的端口号 firewall-cmd --add-service=http --permanent 1. //--permanent表示永久生效,不加这个参数的话只会针对本次执行完命令生效,重...

ACE_LCX
昨天
25
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部