JsonPath basic usage

JsonPath basic usage

This paper describes the basic syntax JsonPath and demonstrates how to use Newtonsoft.Json in.

JsonPath sources

It will be able to see the name you know, this guy, and JSON documents are related, the same as XPath of XML documents, JsonPath provides the ability to parse Json document by using JsonPath, you can easily find the node obtain the desired data , JsonPath is Json version of XPath.

JsonPath grammar

JsonPath grammar is relatively simple, it uses language to develop friendly forms of expression, if you know C-like language, for JsonPath will not feel uncomfortable.

JsonPath grammar points:

  • $ It represents the root element of the document
  • @ The document represents the current element
  • .node_nameOr ['node_name']matching subordinate node
  • [index] Retrieving the array elements
  • [start:end:step] Support array slice syntax
  • * As a wildcard, matching all members
  • .. Sub-recursive wildcard matching all the members of the sub-elements
  • (<expr>) Use the expression
  • ?(<boolean expr>)Filters data

The following table lists all supported syntax, XPath and compare:

XPath JsonPath Explanation
/ $ Document root element
. @ The current element
/ .or[] Matching subordinate elements
.. N/A Matching the parent element, JsonPath does not support this operator
// .. Matches all child elements recursively
* * Wildcard subordinate elements
@ N/A Matching attribute, JsonPath not support this operator
[] [] Subscript operator, acquires the index element, the XPath index starting from 1, JsonPath index starts from 0
| [,] Concatenation operator, the result will be spliced ​​into the plurality of return array, the index may be used or aliases
N/A [start:end:step] Data slicing, XPath does not support
[] ?() Filter expression
N/A () Expressions script, the script uses the underlying engine, XPath does not support
() N/A Grouping, JsonPath not supported

note:

  • JsonPath index starts counting from 0
  • JsonPath string in single quotes, for example: $.store.book[?(@.category=='reference')]the'reference'

JsonPath example

The following are examples of the respective JsonPath code from https://goessner.net/articles/JsonPath/ , the JSON following documents:

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

Next we look at how to parse the document:

XPath JsonPath Result
/store/book/author $.store.book[*].author All the book's author node
//author $..author All author node
/store/* $.store.* All nodes in the store, book an array of nodes and bicycle
/store//price $.store..price All nodes in the store price
//book[3] $..book[2] Match the first three book node
//book[last()] $..book[(@.length-1)],or $..book[-1:] Penultimate match a book node
//book[position()<3] $..book[0,1],or $..book[:2] The first two nodes matching book
//book[isbn] $..book[?(@.isbn)] Filter node field containing isbn
//book[price<10] $..book[?(@.price<10)] Filter price<10nodes
//* $..* Recursive match all child nodes

You can http://jsonpath.com/ carried out to verify the effect of the implementation of JsonPath site.

Usage of Newtonsoft.Json

JsonPath is language-independent expression language, Newtonsoft.Json library provides support for JsonPath, which provides JObject.SelectToken()and JObject.SelectTokens()ways to use JsonPath parse Json document, as follows:

//创建JObject对象
var jObj = JObject.Parse(jsonString);
var books = jObj.SelectToken("$.store.book[?(@.category=='reference')]");

Reference documents:

Guess you like

Origin www.cnblogs.com/youring2/p/10942728.html