JSONPath中的表达式

Posted DaisyLinux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)]

结果将是: