JSONPath中的表達式


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

在本教程中,我們將使用一個示例Json,它在Array中有一些項目。請在我們的JsonPath評估器中復制以下Json

 

 

JsonPath中的表達式

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

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

 

現在讓我們從上面的Json中完成一個簡單的任務。

  • 找出所有頁面大於460的書籍

要創建一個JsonPath,它可以為我們提供所有頁面大於460的書籍,我們必須將問題分成兩部分

  1. 創建JsonPath以檢索所有書籍
  2. 附加表達式以過濾所有頁數大於460的書籍

要獲得所有書籍,我們可以創建一個簡單的JsonPath: $ .books。 現在我們必須在數組書中添加一個表達式。為此,我們將簡單地開始表達式簽名,然后在當前節點上添加一個過濾器。 一個簡單的表達式會是這樣的嗎??(@.pages > 460)。 

如果我們將JsonPath與表達式結合起來,我們將得到:$.books[?(@.pages > 460)]

 

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

JsonPath中的表達式

結果將是頁碼大於460的所有書籍。結果是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 支持所有邏輯運算符。下面是我們可以用來創建表達式的邏輯運算符列表。下面將詳細討論每個邏輯運算符。

操作者 描述
== 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 並了解如何編寫有效的驗證。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM