• 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 并了解如何编写有效的验证。

  • 相关阅读:
    玛里苟斯[清华集训2014 Day1]
    bzoj3585 mex
    Luogu 3793 由乃救爷爷
    Luogu5221 Product
    bzoj1834 [ZJOI2010]network 网络扩容
    CF650C Table Compression
    bzoj3211 花神游历各国
    bzoj1066 [SCOI2007]蜥蜴
    hdu2121 Ice_cream's world II
    Luogu2792 [JSOI2008]小店购物
  • 原文地址:https://www.cnblogs.com/a00ium/p/10323040.html
Copyright © 2020-2023  润新知