Snack3 之 Jsonpath使用

Snack3 之 Jsonpath使用

一、 Snack3 和 JSONPath 介紹

Snack3 是一個(gè)支持JSONPath的JSON框架。JSONPath是一個(gè)很強(qiáng)大的功能,也可以在Java框架中當(dāng)作對(duì)象查詢語(yǔ)言(OQL)來(lái)使用。

<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)文檔樹的操控和構(gòu)建能力
  • 做為中間媒體,方便不同格式互轉(zhuǎn)
  • 高性能Json path查詢(兼容性和性能很贊)
  • 支持序列化、反序列化

二、接口

public class ONode{
    //...
    /**
     * Json path select
     *
     * @param jpath json path express
     * @param useStandard use standard mode(default: false)
     * @param cacheJpath cache json path parsing results
     */
    public ONode select(String jpath,  boolean useStandard, boolean cacheJpath) {
        return JsonPath.eval(this, jpath, useStandard, cacheJpath);
    }

    public ONode select(String jpath,  boolean useStandard) {
        return select(jpath, useStandard, true);
    }

    public ONode select(String jpath) {
        return select(jpath, false);
    }
    //...  
}

默認(rèn)使用緩存JSONPath解析對(duì)象,可提供幾倍性能效果。

三、支持語(yǔ)法

  • 字符串使用單引號(hào),例:['name']
  • 過濾操作用空隔號(hào)隔開,例:[?(@.type == 1)]
支持操作 說(shuō)明
$ 表示根元素
@ 當(dāng)前節(jié)點(diǎn)(做為過濾表達(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>)] 過濾表達(dá)式。 表達(dá)式結(jié)果必須是一個(gè)布爾值。
支持過濾操作符 說(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ù)組的匯總值(新加的)

像這兩種寫法的語(yǔ)義是差不多:

$.store.book[0].title //建議使用這種
$['store']['book'][0]['title']

四、語(yǔ)法示例說(shuō)明

JSONPath 說(shuō)明
$ 根對(duì)象
$[-1] 最后元素
$[:-2] 第0個(gè)至倒數(shù)第2個(gè)
$[1:] 第1個(gè)之后所有元素(0為首個(gè))
$[1,2,3] 集合中1,2,3個(gè)元素(0為首個(gè))

五、接口使用示例

示例1:

讀取對(duì)象的屬性

Entity entity = new Entity(123, new Object());
ONode n = ONode.load(entity);

assert n.select("$.id").getInt() == 123;
assert n.select("$.*").count() == 2;

public static class Entity {
   public int id;
   public String name;
   public Object value;
   public Entity(int id, Object value) { this.id = id; this.value = value; }
   public Entity(String name) { this.name = name; }
}

示例2

讀取集合多個(gè)元素的某個(gè)屬性

List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity("wenshao"));
entities.add(new Entity("ljw2083"));
ONode n = ONode.load(entities);

List<String> names = n.select("$.name").toObject(List.class); 
assert names.size() == 2;

示例3

返回集合中多個(gè)元素

List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity("wenshao"));
entities.add(new Entity("ljw2083"));
entities.add(new Entity("Yako"));
ONode n = ONode.load(entities);

List<Entity> result = n.select("$[1,2]").toObject((new ArrayList<Entity>() {}).getClass());
assert result.size() == 2;

示例4

按范圍返回集合的子集

List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity("wenshao"));
entities.add(new Entity("ljw2083"));
entities.add(new Entity("Yako"));
ONode n = ONode.load(entities);

List<Entity> result = n.select("$[0:2]").toObject((new ArrayList<Entity>(){}).getClass());
assert result.size() == 2;

示例5

通過條件過濾,返回集合的子集

List<Entity> entities = new ArrayList<Entity>();
entities.add(new Entity(1001, "ljw2083"));
entities.add(new Entity(1002, "wenshao"));
entities.add(new Entity(1003, "yakolee"));
entities.add(new Entity(1004, null));
ONode n = ONode.load(entities);

ONode rst = n.select("$[?($.id in [1001,1002])]");
assert rst.count() == 2;

示例6

根據(jù)屬性值過濾條件判斷是否返回對(duì)象,修改對(duì)象,數(shù)組屬性添加元素

Entity entity = new Entity(1001, "ljw2083");
ONode n = ONode.load(entity);

assert n.select("$[?(id == 1001)]").isObject();
assert n.select("$[?(id == 1002)]").isNull();

n.select("$").set("id",123456);
assert n.get("id").getInt() == 123456;

n.get("value").add(1).add(2).add(3);
assert n.get("value").count() == 3;

示例7

Map root = Collections.singletonMap("company",
        Collections.singletonMap("departs",
                Arrays.asList(
                        Collections.singletonMap("id",
                                1001),
                        Collections.singletonMap("id",
                                1002),
                        Collections.singletonMap("id", 1003)
                )
        ));

ONode n = ONode.load(root);

List<Object> ids = n.select("$..id").toObject(List.class);
assertEquals(3l, ids.size());
assertEquals(1001l, ids.get(0));
assertEquals(1002l, ids.get(1));
assertEquals(1003l, ids.get(2));

具體用例測(cè)試請(qǐng)看下面:

String jsonStr = "{\n" +
        "    \"store\": {\n" +
        "        \"bicycle\": {\n" +
        "            \"color\": \"red\",\n" +
        "            \"price\": 19.95\n" +
        "        },\n" +
        "        \"book\": [\n" +
        "            {\n" +
        "                \"author\": \"劉慈欣\",\n" +
        "                \"price\": 8.95,\n" +
        "                \"category\": \"科幻\",\n" +
        "                \"title\": \"三體\"\n" +
        "            },\n" +
        "            {\n" +
        "                \"author\": \"itguang\",\n" +
        "                \"price\": 12.99,\n" +
        "                \"category\": \"編程語(yǔ)言\",\n" +
        "                \"title\": \"go語(yǔ)言實(shí)戰(zhàn)\"\n" +
        "            }\n" +
        "        ]\n" +
        "    }\n" +
        "}";

ONode o = ONode.load(jsonStr);

//得到所有的書
ONode books = o.select("$.store.book");
System.out.println("books=::" + books);

//得到所有的書名
ONode titles = o.select("$.store.book.title");
System.out.println("titles=::" + titles);

//第一本書title
ONode title = o.select("$.store.book[0].title");
System.out.println("title=::" + title);

//price大于10元的book
ONode list = o.select("$.store.book[?(price > 10)]");
System.out.println("price大于10元的book=::" + list);

//price大于10元的title
ONode list2 = o.select("$.store.book[?(price > 10)].title");
System.out.println("price大于10元的title=::" + list2);

//category(類別)為科幻的book
ONode list3 = o.select("$.store.book[?(category == '科幻')]");
System.out.println("category(類別)為科幻的book=::" + list3);


//bicycle的所有屬性值
ONode values = o.select("$.store.bicycle.*");
System.out.println("bicycle的所有屬性值=::" + values);


//bicycle的color和price屬性值
ONode read = o.select("$.store.bicycle['color','price']");
System.out.println("bicycle的color和price屬性值=::" + read);

打印結(jié)果

books=::[{"author":"劉慈欣","price":8.95,"category":"科幻","title":"三體"},{"author":"itguang","price":12.99,"category":"編程語(yǔ)言","title":"go語(yǔ)言實(shí)戰(zhàn)"}]
titles=::["三體","go語(yǔ)言實(shí)戰(zhàn)"]
title=::"三體"
price大于10元的book=::[{"author":"itguang","price":12.99,"category":"編程語(yǔ)言","title":"go語(yǔ)言實(shí)戰(zhàn)"}]
price大于10元的title=::["go語(yǔ)言實(shí)戰(zhàn)"]
category(類別)為科幻的book=::[{"author":"劉慈欣","price":8.95,"category":"科幻","title":"三體"}]
bicycle的所有屬性值=::["red",19.95]
bicycle的color和price屬性值=::["red",19.95]
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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