JSONPath入門(mén)之Snack3篇

Snack3 for java

一個(gè)微型JSON框架

基于jdk8,60kb。有序列化反序列化、解析和轉(zhuǎn)換、支持 Json path 查詢。

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

Snack3 借鑒了 Javascript 所有變量由 var 申明,及 Xml dom 一切都是 Node 的設(shè)計(jì)。其下一切數(shù)據(jù)都以ONode表示,ONode也即 One node 之意,代表任何類型,也可以轉(zhuǎn)換為任何類型。

  • 強(qiáng)調(diào)文檔樹(shù)的操控和構(gòu)建能力
  • 做為中間媒體,方便不同格式互轉(zhuǎn)
  • 高性能Json path查詢(兼容性和性能很贊)
  • 支持序列化、反序列化

今天用它來(lái)試一下JSONPath入門(mén)

一、JSONPath語(yǔ)法介紹

  • 字符串使用單引號(hào),例:['name']
  • 過(guò)濾操作用空隔號(hào)隔開(kāi),例:[?(@.type == 1)]
支持操作 說(shuō)明
$ 表示根元素
@ 當(dāng)前節(jié)點(diǎn)(做為過(guò)濾表達(dá)式的謂詞使用)
* 通用配配符,可以表示一個(gè)名字或數(shù)字。
.. 深層掃描。 可以理解為遞歸搜索。
.<name> 表示一個(gè)子節(jié)點(diǎn)
['<name>' (, '<name>')] 表示一個(gè)或多個(gè)子節(jié)點(diǎn)
[<number> (, <number>)] 表示一個(gè)或多個(gè)數(shù)組下標(biāo)(負(fù)號(hào)為倒數(shù))
[start:end] 數(shù)組片段,區(qū)間為[start,end),不包含end(負(fù)號(hào)為倒數(shù))
[?(<expression>)] 過(guò)濾表達(dá)式。 表達(dá)式結(jié)果必須是一個(gè)布爾值。
支持過(guò)濾操作符 說(shuō)明
== left等于right(注意1不等于'1')
!= 不等于
< 小于
<= 小于等于
> 大于
>= 大于等于
=~ 匹配正則表達(dá)式[?(@.name =~ /foo.*?/i)]
in 左邊存在于右邊 [?(@.size in ['S', 'M'])]
nin 左邊不存在于右邊
支持尾部函數(shù) 說(shuō)明
min() 計(jì)算數(shù)字?jǐn)?shù)組的最小值
max() 計(jì)算數(shù)字?jǐn)?shù)組的最大值
avg() 計(jì)算數(shù)字?jǐn)?shù)組的平均值
sum() 計(jì)算數(shù)字?jǐn)?shù)組的匯總值(新加的)

二、準(zhǔn)備JSON數(shù)據(jù)

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

四、控制臺(tái)輸出

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}]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容