分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

JSONPath中的表达式

发布时间:2023-09-06 02:31责任编辑:董明明关键词:JSONP

在JsonPath中使用表达式是一个非常好的功能,可以使用简洁和复杂的JsonPathJsonPath中的表达式基本上是评估为布尔值的代码片段。基于结果,仅选择满足标准的节点。让我们看一下它的更多内容,但在此之前请确保您已经完成了关于JsonJsonPath基础知识的教程

  • JSON
  • Jsonpath

在本教程中,我们将使用一个示例Json,它在Array中有一些项目。请在我们的JsonPath评估器中复制以下Json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
{
"books": [
{
"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": "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"
},
{
"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中的表达式

在JsonPath表达式是最强大的功能之一JsonPath。请注意,表达式也可用于XpathCSS选择器。表达式可帮助您创建评估为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中,只需输入此表达式并查看结果。如下图所示

结果将是页码大于460的所有书籍。结果是Json的结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[
{
"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"
}
]
[ ?{ ???"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支持所有逻辑运算符。下面是我们可以用来创建表达式的逻辑运算符列表。下面将详细讨论每个逻辑运算符。

操作者<
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved