Snack3 for java

一个微型JSON框架

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

  1. <dependency>
  2. <groupId>org.noear</groupId>
  3. <artifactId>snack3</artifactId>
  4. <version>3.1.5.9</version>
  5. </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数据

  1. {
  2. "store": {
  3. "book": [{
  4. "category": "reference",
  5. "author": "Nigel Rees",
  6. "title": "Sayings of the Century",
  7. "price": 8.95
  8. }, {
  9. "category": "fiction",
  10. "author": "Evelyn Waugh",
  11. "title": "Sword of Honour",
  12. "price": 12.99
  13. }, {
  14. "category": "fiction",
  15. "author": "Herman Melville",
  16. "title": "Moby Dick",
  17. "isbn": "0-553-21311-3",
  18. "price": 8.99
  19. }, {
  20. "category": "fiction",
  21. "author": "J. R. R. Tolkien",
  22. "title": "The Lord of the Rings",
  23. "isbn": "0-395-19395-8",
  24. "price": 22.99
  25. }],
  26. "bicycle": {
  27. "color": "red",
  28. "price": 19.95
  29. }
  30. },
  31. "expensive": 10
  32. }

三、演示代码

  1. @Test
  2. public void demo1(){
  3. 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}";
  4. ONode n = ONode.load(json);
  5. ONode t1 = n.select("$.store.book [0].title");
  6. System.out.println("\nt1:" + t1);
  7. ONode t2 = n.select("$['store']['book'][0]['title']");
  8. System.out.println("\nt2:" + t2);
  9. ONode t3 = n.select("$.store.book[*].author");
  10. System.out.println("\nt3:" + t3);
  11. ONode t4 = n.select("$..author");
  12. System.out.println("\nt4:" + t4);
  13. ONode t5 = n.select("$.store.*");
  14. System.out.println("\nt5:" + t5);
  15. ONode t6 = n.select("$.store..price");
  16. System.out.println("\nt6:" + t6);
  17. ONode t7 = n.select("$..book[2]");
  18. System.out.println("\nt7:" + t7);
  19. ONode t8 = n.select("$..book[-2]");
  20. System.out.println("\nt8:" + t8);
  21. ONode t9 = n.select("$..book[0,1]");
  22. System.out.println("\nt9:" + t9);
  23. ONode ta = n.select("$..book[:2]");
  24. System.out.println("\nta:" + ta);
  25. ONode tb = n.select("$..book[1:2]");
  26. System.out.println("\ntb:" + tb);
  27. ONode tc = n.select("$..book[-2:]");
  28. System.out.println("\ntc:" + tc);
  29. ONode td = n.select("$..book[2:]");
  30. System.out.println("\ntd:" + td);
  31. ONode te = n.select("$..book[?(@.isbn)]");
  32. System.out.println("\nte:" + te);
  33. ONode tf = n.select("$.store.book[?(@.price < 10)]");
  34. System.out.println("\ntf:" + tf);
  35. ONode tg = n.select("$..book[?(@.author =~ /.*REES/i)]");
  36. System.out.println("\ntg:" + tg);
  37. ONode th = n.select("$..*");
  38. System.out.println("\nth:" + th);
  39. ONode ti = n.select("$..book[?(@.price <= $.expensive)]");
  40. System.out.println("\nti:" + ti);
  41. }

四、控制台输出

  1. t1:"Sayings of the Century"
  2. t2:"Sayings of the Century"
  3. t3:["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
  4. t4:["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
  5. 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}]
  6. t6:[8.95,12.99,8.99,22.99,19.95]
  7. t7:[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]
  8. t8:[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]
  9. 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}]
  10. 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}]
  11. tb:[{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}]
  12. 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}]
  13. 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}]
  14. 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}]
  15. 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}]
  16. tg:[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95}]
  17. 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]
  18. 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篇的更多相关文章

  1. ElasticSearch入门 第五篇:使用C#查询文档

    这是ElasticSearch 2.4 版本系列的第五篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  2. Python 正则表达式入门(中级篇)

    Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...

  3. Python 正则表达式入门(初级篇)

    Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. 转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达 ...

  4. Qt入门之基础篇 ( 二 ) :Qt项目建立、编译、运行和发布过程解析

    转载请注明出处:CN_Simo. 题解: 本篇内容主讲Qt应用从创建到发布的整个过程,旨在帮助读者能够快速走进Qt的世界. 本来计划是讲解Qt源码静态编译,如此的话读者可能并不能清楚地知道为何要静态编 ...

  5. Qt入门之基础篇(三):掌握Qt4的静态编译基本方法

    转载载请注明出处:CN_Simo. 导语: 前两章都提到过“静态编译”(Static Compilation),在Windows下一次静态编译差不多需要长达三个小时才能完成,而且还非常容易由于各种原因 ...

  6. .NET Core实战项目之CMS 第五章 入门篇-Dapper的快速入门看这篇就够了

    写在前面 上篇文章我们讲了如在在实际项目开发中使用Git来进行代码的版本控制,当然介绍的都是比较常用的功能.今天我再带着大家一起熟悉下一个ORM框架Dapper,实例代码的演示编写完成后我会通过Git ...

  7. Spring Boot 入门之基础篇(一)

    原文地址:Spring Boot 入门之基础篇(一) 博客地址:http://www.extlight.com 一.前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是 ...

  8. 转载 Python 正则表达式入门(中级篇)

    Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...

  9. 转载 Python 正则表达式入门(初级篇)

    Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写.转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式 ...

随机推荐

  1. vue 父子组件通信详解

    这是一篇详细讲解vue父子组件之间通信的文章,初始学习vue的时候,总是搞不清楚几个情况 通过props在父子组件传值时,v-bind:data="data",props接收的到底 ...

  2. spring cloud 2.x版本 Eureka Client服务提供者教程

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 1 创建eureka client 1.1 新建Srping boot工程:eureka-c ...

  3. 完美解决Python与anaconda之间的冲突问题

    anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.因为包含了大量的科学包,Anaconda 的下载文件比较大(约 515 MB),如果 ...

  4. shell 字符串比较与脚本 too many arguments 报错

    一.问题 最近在写 shell 脚本的时候,遇到了一些小问题,就是我在判断一个字符串是否为空的时候经常报下面的错,程序是正常执行了,但是有这个提示很蛋疼,下面就是看看是什么问题导致的? [: too ...

  5. CSPS模拟 86

    看见异或两个字就孩怕 T1 按位? T2 这道异或稍水啊233 貌似可以打表找找规律 emm七种转移,有重复刷表 优化一下? T3 skyh已经接了2杯水了(实际情况他已经ak了) cbx开始抬头傻笑 ...

  6. Ubuntu16.04下nvidia驱动+nvidia-docker+cuda9+cudnn7安装

    一.宿主机安装nvidia驱动 打开终端,先删除旧的驱动: sudo apt-get purge nvidia* 禁用自带的 nouveau nvidia驱动 sudo gedit /etc/modp ...

  7. 使用Typescript重构axios(二十)——请求取消功能:实现第一种使用方式

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  8. 考试T3麻将

    这题就是一个简单的暴力,但考试的时候不知道脑子在想什么,什么都没打出来,也许是我想的太多了... 这道题对于不会打麻将的人来说还是有点难理解规则的,我没说过我会打麻将,这里是题目链接. 20分思路,利 ...

  9. 大数据之路week01--自学之集合_2(列表迭代器 ListIterator)

    列表迭代器: ListIterator listerator():List集合特有的迭代器 该迭代器继承了Iterator迭代器,所以,就可以直接使用hasNext()和next()方法 特有功能: ...

  10. m96-97 lsc nc赛

    这一次 lsc 再一次一道题都没AC,看来lsc已经凉了! 出了分,旁边的_LH大喊了一声 “woc,lsc,你真是太垃圾!”...........“好吧!” 我确实很垃圾!(大佬这次都没考,所以我更 ...