Grammar:
JsonPath |
Describe |
$ |
Root node |
@ |
Current node |
. or[] |
Child nodes |
.. |
Select all nodes that meet the criteria |
* |
All nodes |
[] |
Iterator indicator, array subscript |
[,] |
Supports long selection in iterators |
[Start:end:step] |
Array Slice Operators |
? () |
Support filtering operations |
() |
Supports expression evaluation |
Required JAR Packages:
commons-lang-2.6. Jarjson-path-0.8. 1 . Jarjson-smart-1.1. 1. jar
For the following JSON, the use of Jsonpath is explained by an example
{"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, "ISBN":"0-553-21311-3" } ], "Bicycle": { "Color":"Red", " Price":19.95 } }}
Code:
private static void Jsonpathtest () {Jsonobject json = jsontest ();//Call the Custom Jsontest () method to get the JSON object, generating the above json//output BOOK[0] The author value of String author = jsonpath.read (JSON, "$.store.book[0].author");//output All author values, using iterator iteration list<string > authors = Jsonpath.read (JSON, "$.store.book[*].author");//output BOOK[*] category = = ' reference ' Booklist<object > books = Jsonpath.read (JSON, "$.store.book[?" ( @.category = = ' reference ')] [//output BOOK[*] price>10 booklist<object> books = Jsonpath.read (JSON, "$"). Store.book[? ( @.price>10)] [//Output BOOK[*] contains the ISBN element booklist<object> books = Jsonpath.read (JSON, "$.store.book[? ( @.isbn)];//output the value of all price in the JSON list<double> prices = Jsonpath.read (JSON, "$"). Price ");//You can edit a path in advance and use it multiple times JsonPath path = Jsonpath.compile (" $.store.book[*] "); list<object> books = Path.read (JSON);}