Getting the JSONPath Snack3 articles

Snack3 for java

A miniature JSON framework

Based jdk8,60kb. There serialization deserialization, parsing and conversion, support Json path queries.

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

Snack3 signed by Javascriptall of the variables varstated, and Xml domeverything is Nodedesign. Under which all data are to ONoderepresent, ONodenamely One nodeItaly, on behalf of any type can be converted to any type.

  • Manipulation of the document tree and emphasize building skills
  • As a media center, easy conversion between different formats
  • High-performance Json pathquery (compatibility and performance is praise)
  • stand by序列化、反序列化

Today, we use it to try JSONPath entry

A, JSONPath grammar description

  • String use single quotes, for example: [ 'name']
  • No filtration operation with an empty compartment spaced, for example: [(@ type == 1.)?]
Support Operation Explanation
$ It represents the root element
@ The current node (predicate as a filter expression to use)
* Wildcards generic ligand, may represent a name or number.
.. Deep Scan. It can be understood as a recursive search.
.<name> It represents a child node
['<name>' (, '<name>')] It represents one or more child node
[<number> (, <number>)] Represents one or more array subscript (negative reciprocal number)
[start:end] Array section, interval [start, end), does not include end (negative reciprocal number)
[?(<expression>)] Filter expression. Expression must evaluate to a Boolean value.
Filtering operator support Explanation
== equals left right (note 1 is not equal to '1')
!= not equal to
< Less than
<= Less than or equal
> more than the
>= greater or equal to
=~ Match the regular expression [? (@. Name = ~ /foo.*?/i)]
in On the left there is the right [? (@. Size in [ 'S', 'M'])]
nin The left does not exist in the right
Support functions tail Explanation
min() The minimum value of the array of numbers
max() Calculating the maximum number of the array
avg() Number average calculation array
sum() Summary digital value calculation arrays (newly added)

 Second, prepare JSON data

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

Third, the demo code

@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);
}

Fourth, the console output

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}]

Guess you like

Origin www.cnblogs.com/noear/p/11959040.html