JSONPath入门之Snack3篇
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}]
JSONPath入门之Snack3篇的更多相关文章
- ElasticSearch入门 第五篇:使用C#查询文档
这是ElasticSearch 2.4 版本系列的第五篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- Python 正则表达式入门(中级篇)
Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...
- Python 正则表达式入门(初级篇)
Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. 转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达 ...
- Qt入门之基础篇 ( 二 ) :Qt项目建立、编译、运行和发布过程解析
转载请注明出处:CN_Simo. 题解: 本篇内容主讲Qt应用从创建到发布的整个过程,旨在帮助读者能够快速走进Qt的世界. 本来计划是讲解Qt源码静态编译,如此的话读者可能并不能清楚地知道为何要静态编 ...
- Qt入门之基础篇(三):掌握Qt4的静态编译基本方法
转载载请注明出处:CN_Simo. 导语: 前两章都提到过“静态编译”(Static Compilation),在Windows下一次静态编译差不多需要长达三个小时才能完成,而且还非常容易由于各种原因 ...
- .NET Core实战项目之CMS 第五章 入门篇-Dapper的快速入门看这篇就够了
写在前面 上篇文章我们讲了如在在实际项目开发中使用Git来进行代码的版本控制,当然介绍的都是比较常用的功能.今天我再带着大家一起熟悉下一个ORM框架Dapper,实例代码的演示编写完成后我会通过Git ...
- Spring Boot 入门之基础篇(一)
原文地址:Spring Boot 入门之基础篇(一) 博客地址:http://www.extlight.com 一.前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是 ...
- 转载 Python 正则表达式入门(中级篇)
Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...
- 转载 Python 正则表达式入门(初级篇)
Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写.转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式 ...
随机推荐
- fenby C语言 P25
二维数组 #include <stdio.h> int main(){ int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; int sum=0, ...
- Display(显示) 与 Visibility(可见性)
display属性设置一个元素应如何显示,visibility属性指定一个元素应可见还是隐藏. 隐藏元素 - display:none或visibility:hidden 隐藏一个元素可以通过把dis ...
- ArangoDB数据导入
目录 arangoimp方法 参数解析 实例展示 python方法 单条导入 批量导入 1.arangoimp方法 参数解析 全局配置部分(Global configuration) --backsl ...
- GStreamer基础教程12 - 常用命令工具
摘要 GStreamer提供了不同的命令行工具用于快速的查看信息以及验证Pipeline的是否能够正确运行,在平时的开发过程中,我们也优先使用GStreamer的命令行工具验证,再将Pipeline集 ...
- 关于ArcGIS的OBJECTID生成策略拙见
目录 诉求SDEOBJECTIDArcMap编辑重置OBJECTID 诉求 非GIS专业的人员可能很难理解ArcSDE中的表OBJECTID的重要性,要么总想着自己动手去维护,要么就想直接忽略它,导致 ...
- 中文预训练模型ERNIE2.0模型下载及安装
2019年7月,百度ERNIE再升级,发布持续学习的语义理解框架ERNIE 2.0,及基于此框架的ERNIE 2.0预训练模型, 它利用百度海量数据和飞桨(PaddlePaddle)多机多卡高效训练优 ...
- linux IMX6 汇编点亮一个LED灯
驱动Linux引脚与驱动STM32其实是一样的,都是在操作寄存器,在相应的寄存器上附上相应的值即可驱动. IMX6U手册上有各个管脚的命名,跟STM32不同,IOMUXC_SW_MUC_CTL_PAD ...
- 在虚拟机上的关于FTP FTP访问模式(匿名)
小知识 nfs和ftp类似另外一款共享软件 用21号端口 传控制 20号端口 传数据 Windows和虚拟机之间 接下来进行实验 首先在yum资源库中下载 输入命令:yum install vsft ...
- [考试反思]0822NOIP模拟测试29:延续
想保持优秀很困难 但是想持续垫底却很简单 但是你不想垫底的话持续垫底也很容易... 分AB卷,A卷共15人. skyh,tdcp,kx155,B哥145... 我:35,倒数第一. 板子专题,爆零快乐 ...
- ElasticSearch(四):基本搜索
ElasticSearch(四):基本搜索 学习课程链接<Elasticsearch核心技术与实战> URI Search 使用HTTP的GET方法,在URL中使用查询参数进行查询. GE ...