JSONPath中的表達式

在JsonPath中使用表達式是一個很是好的功能,能夠使用簡潔和複雜的JsonPathJsonPath中的表達式基本上是評估爲布爾值的代碼片斷。基於結果,僅選擇知足標準的節點。讓咱們看一下它的更多內容,但在此以前請確保您已經完成了關於JsonJsonPath基礎知識的教程javascript

在本教程中,咱們將使用一個示例Json,它在Array中有一些項目。請在咱們的JsonPath評估器中複製如下Jsonhtml

 

 

JsonPath中的表達式

在JsonPath表達式是最強大的功能之一JsonPath請注意,表達式也可用於Xpath CSS選擇器表達式可幫助您建立評估爲true或false的條件。JsonPath中建立表達式以前,您必須瞭解兩個重要的符號java

  • 問號,標記表達式的開頭。使用的語法[?(表達)]
  • [? (Expression)]
  • @: 在符號處表示正在處理的當前節點。語法使用$.books[?(@.price > 100)]

 

如今讓咱們從上面的Json中完成一個簡單的任務。git

  • 找出全部頁面大於460的書籍

要建立一個JsonPath,它能夠爲咱們提供全部頁面大於460的書籍,咱們必須將問題分紅兩部分es6

  1. 建立JsonPath以檢索全部書籍
  2. 附加表達式以過濾全部頁數大於460的書籍

要得到全部書籍,咱們能夠建立一個簡單的JsonPath: $ .books。 如今咱們必須在數組書中添加一個表達式。爲此,咱們將簡單地開始表達式簽名,而後在當前節點上添加一個過濾器。 一個簡單的表達式會是這樣的嗎??(@.pages > 460)。 github

若是咱們將JsonPath與表達式結合起來,咱們將獲得:$.books[?(@.pages > 460)]
web

 

JsonPath Evaluator中只需輸入此表達式並查看結果。以下圖所示編程

JsonPath中的表達式

結果將是頁碼大於460的全部書籍。結果是Json的結果json

 

[
  {
    "isbn": "9781593275846",
    "title": "Eloquent JavaScript, Second Edition",
    "subtitle": "A Modern Introduction to Programming",
    "author": "Marijn Haverbeke",
    "published": "2014-12-14T00:00:00.000Z",
    "publisher": "No Starch Press",
    "pages": 472,
    "description": "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.",
    "website": "http://eloquentjavascript.net/"
  },
  {
    "isbn": "9781449337711",
    "title": "Designing Evolvable Web APIs with ASP.NET",
    "subtitle": "Harnessing the Power of the Web",
    "author": "Glenn Block, et al.",
    "published": "2014-04-07T00:00:00.000Z",
    "publisher": "O'Reilly Media",
    "pages": 538,
    "description": "Design and build Web APIs for a broad range of clients—including browsers and mobile devices—that can adapt to change over time. This practical, hands-on guide takes you through the theory and tools you need to build evolvable HTTP services with Microsoft’s ASP.NET Web API framework. In the process, you’ll learn how design and implement a real-world Web API.",
    "website": "http://chimera.labs.oreilly.com/books/1234000001708/index.html"
  }
]

 

 

JsonPath中的邏輯運算符

就像任何編程語言同樣,JsonPath 支持全部邏輯運算符。下面是咱們能夠用來建立表達式的邏輯運算符列表。下面將詳細討論每一個邏輯運算符。api

操做者 描述
== left等於right(注意1不等於'1')。
!= 左邊不等於右邊。
< 左邊不是正確的。
<= 左邊小於或等於右邊。
> 左邊大於右邊。
> = left大於或等於right。

 
嘗試以上全部示例,並嘗試根據您的須要建立更多表達式。這樣,您將瞭解有關JsonPath表達式的更多信息

 

 

等於JsonPath中的(==)運算符

顧名思義,操做員檢查左側是否等於右側。讓咱們找出全部有352頁的書籍。這是JsonPath

JsonPath $.books[?(@.pages == 352)]

結果將是:

 

[
  {
    "isbn": "9781593277574",
    "title": "Understanding ECMAScript 6",
    "subtitle": "The Definitive Guide for JavaScript Developers",
    "author": "Nicholas C. Zakas",
    "published": "2016-09-03T00:00:00.000Z",
    "publisher": "No Starch Press",
    "pages": 352,
    "description": "ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript.",
    "website": "https://leanpub.com/understandinges6/read"
  }
]

 

 

不等於JsonPath中的(!=)運算符

當咱們想要根據條件排除 一組特定的值時,咱們使用不等於運算符。讓咱們顛倒上面的例子,找到頁碼不等於352的全部書籍

JsonPath: $.books[?(@.pages != 352)]

結果將是:

 

[
  {
    "isbn": "9781593275846",
    "title": "Eloquent JavaScript, Second Edition",
    "subtitle": "A Modern Introduction to Programming",
    "author": "Marijn Haverbeke",
    "published": "2014-12-14T00:00:00.000Z",
    "publisher": "No Starch Press",
    "pages": 472,
    "description": "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.",
    "website": "http://eloquentjavascript.net/"
  },
  {
    "isbn": "9781449331818",
    "title": "Learning JavaScript Design Patterns",
    "subtitle": "A JavaScript and jQuery Developer's Guide",
    "author": "Addy Osmani",
    "published": "2012-07-01T00:00:00.000Z",
    "publisher": "O'Reilly Media",
    "pages": 254,
    "description": "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.",
    "website": "http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/"
  },
  {
    "isbn": "9781449365035",
    "title": "Speaking JavaScript",
    "subtitle": "An In-Depth Guide for Programmers",
    "author": "Axel Rauschmayer",
    "published": "2014-02-01T00:00:00.000Z",
    "publisher": "O'Reilly Media",
    "pages": 460,
    "description": "Like it or not, JavaScript is everywhere these days-from browser to server to mobile-and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.",
    "website": "http://speakingjs.com/"
  },
  {
    "isbn": "9781491950296",
    "title": "Programming JavaScript Applications",
    "subtitle": "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
    "author": "Eric Elliott",
    "published": "2014-07-01T00:00:00.000Z",
    "publisher": "O'Reilly Media",
    "pages": 254,
    "description": "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your code base grows.",
    "website": "http://chimera.labs.oreilly.com/books/1234000000262/index.html"
  },
  {
    "isbn": "9781491904244",
    "title": "You Don't Know JS",
    "subtitle": "ES6 & Beyond",
    "author": "Kyle Simpson",
    "published": "2015-12-27T00:00:00.000Z",
    "publisher": "O'Reilly Media",
    "pages": 278,
    "description": "No matter how much experience you have with JavaScript, odds are you don’t fully understand the language. As part of the 'You Don’t Know JS' series, this compact guide focuses on new features available in ECMAScript 6 (ES6), the latest version of the standard upon which JavaScript is built.",
    "website": "https://github.com/getify/You-Dont-Know-JS/tree/master/es6%20&%20beyond"
  },
  {
    "isbn": "9781449325862",
    "title": "Git Pocket Guide",
    "subtitle": "A Working Introduction",
    "author": "Richard E. Silverman",
    "published": "2013-08-02T00:00:00.000Z",
    "publisher": "O'Reilly Media",
    "pages": 234,
    "description": "This pocket guide is the perfect on-the-job companion to Git, the distributed version control system. It provides a compact, readable introduction to Git for new users, as well as a reference to common commands and procedures for those of you with Git experience.",
    "website": "http://chimera.labs.oreilly.com/books/1230000000561/index.html"
  },
  {
    "isbn": "9781449337711",
    "title": "Designing Evolvable Web APIs with ASP.NET",
    "subtitle": "Harnessing the Power of the Web",
    "author": "Glenn Block, et al.",
    "published": "2014-04-07T00:00:00.000Z",
    "publisher": "O'Reilly Media",
    "pages": 538,
    "description": "Design and build Web APIs for a broad range of clients—including browsers and mobile devices—that can adapt to change over time. This practical, hands-on guide takes you through the theory and tools you need to build evolvable HTTP services with Microsoft’s ASP.NET Web API framework. In the process, you’ll learn how design and implement a real-world Web API.",
    "website": "http://chimera.labs.oreilly.com/books/1234000001708/index.html"
  }
]

  

 

JsonPath中的小於(<)運算符

小於運算符,顧名思義將返回小於右邊給出的值的全部值。讓咱們找出頁數低於352的全部書籍。

 

JsonPath: $.books[?(@.pages < 352)]

結果將是:

 

 1 [
 2   {
 3     "isbn": "9781449331818",
 4     "title": "Learning JavaScript Design Patterns",
 5     "subtitle": "A JavaScript and jQuery Developer's Guide",
 6     "author": "Addy Osmani",
 7     "published": "2012-07-01T00:00:00.000Z",
 8     "publisher": "O'Reilly Media",
 9     "pages": 254,
10     "description": "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.",
11     "website": "http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/"
12   },
13   {
14     "isbn": "9781491950296",
15     "title": "Programming JavaScript Applications",
16     "subtitle": "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
17     "author": "Eric Elliott",
18     "published": "2014-07-01T00:00:00.000Z",
19     "publisher": "O'Reilly Media",
20     "pages": 254,
21     "description": "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your code base grows.",
22     "website": "http://chimera.labs.oreilly.com/books/1234000000262/index.html"
23   },
24   {
25     "isbn": "9781491904244",
26     "title": "You Don't Know JS",
27     "subtitle": "ES6 & Beyond",
28     "author": "Kyle Simpson",
29     "published": "2015-12-27T00:00:00.000Z",
30     "publisher": "O'Reilly Media",
31     "pages": 278,
32     "description": "No matter how much experience you have with JavaScript, odds are you don’t fully understand the language. As part of the 'You Don’t Know JS' series, this compact guide focuses on new features available in ECMAScript 6 (ES6), the latest version of the standard upon which JavaScript is built.",
33     "website": "https://github.com/getify/You-Dont-Know-JS/tree/master/es6%20&%20beyond"
34   },
35   {
36     "isbn": "9781449325862",
37     "title": "Git Pocket Guide",
38     "subtitle": "A Working Introduction",
39     "author": "Richard E. Silverman",
40     "published": "2013-08-02T00:00:00.000Z",
41     "publisher": "O'Reilly Media",
42     "pages": 234,
43     "description": "This pocket guide is the perfect on-the-job companion to Git, the distributed version control system. It provides a compact, readable introduction to Git for new users, as well as a reference to common commands and procedures for those of you with Git experience.",
44     "website": "http://chimera.labs.oreilly.com/books/1230000000561/index.html"
45   }
46 ]
View Code

 

 

小於等於JsonPath中的(<=)運算符

此運算符將容許您獲取小於或等於給定值的全部值。讓咱們找出全部頁面小於或等於352的書籍。 

JJsonPath: $.books[?(@.pages < 352)]

結果將是:

 

 

JsonPath中的大於(>)運算符

大於運算符將使您得到大於左側值的全部值。讓咱們找到全部頁數超過460的書籍。

JsonPath: $.books[?(@.pages > 460)]

結果將是:

 

 

大於等於JsonPath中的(> =)運算符

大於等於將使您得到等於或大於右側值的全部值。讓咱們獲得全部書籍的頁面大於或等於460

JsonPath: $.books[?(@.pages >= 460)]

結果將是:

 1 [
 2   {
 3     "isbn": "9781593275846",
 4     "title": "Eloquent JavaScript, Second Edition",
 5     "subtitle": "A Modern Introduction to Programming",
 6     "author": "Marijn Haverbeke",
 7     "published": "2014-12-14T00:00:00.000Z",
 8     "publisher": "No Starch Press",
 9     "pages": 472,
10     "description": "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.",
11     "website": "http://eloquentjavascript.net/"
12   },
13   {
14     "isbn": "9781449365035",
15     "title": "Speaking JavaScript",
16     "subtitle": "An In-Depth Guide for Programmers",
17     "author": "Axel Rauschmayer",
18     "published": "2014-02-01T00:00:00.000Z",
19     "publisher": "O'Reilly Media",
20     "pages": 460,
21     "description": "Like it or not, JavaScript is everywhere these days-from browser to server to mobile-and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.",
22     "website": "http://speakingjs.com/"
23   },
24   {
25     "isbn": "9781449337711",
26     "title": "Designing Evolvable Web APIs with ASP.NET",
27     "subtitle": "Harnessing the Power of the Web",
28     "author": "Glenn Block, et al.",
29     "published": "2014-04-07T00:00:00.000Z",
30     "publisher": "O'Reilly Media",
31     "pages": 538,
32     "description": "Design and build Web APIs for a broad range of clients—including browsers and mobile devices—that can adapt to change over time. This practical, hands-on guide takes you through the theory and tools you need to build evolvable HTTP services with Microsoft’s ASP.NET Web API framework. In the process, you’ll learn how design and implement a real-world Web API.",
33     "website": "http://chimera.labs.oreilly.com/books/1234000001708/index.html"
34   }
35 ]
View Code

 

在接下來的章節中,咱們將Rest-Assured中使用JsonPath 並瞭解如何編寫有效的驗證。

相關文章
相關標籤/搜索