Use of Jsonpath

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.