REST Assured 60 - Learn To Write JsonPath Expressions Or JsonPath Syntax

REST Assured 系列汇总 之 REST Assured 60 - Learn To Write JsonPath Expressions Or JsonPath Syntax

介绍

前面我们已经介绍了 JsonPath,以及简单的和嵌套的 JSON Object,JSON Array 的 JsonPath,可以参考下面文章:

What Is JsonPath And How To Create It For Simple And Nested JSON Object?

How To Create JsonPath For Simple And Nested JSON Array?

前提条件

默认 Rest Assured 是包括 JsonPath 依赖库的。所以只要添加了 Rest Assured 依赖库,就没有必要再添加 JsonPath 依赖库了。

<!-- REST Assured -->
<dependency>
  <groupId>io.rest-assured</groupId>
  <artifactId>rest-assured</artifactId>
  <version>4.4.0</version>
</dependency>

Rest Assured JsonPath 基础概念

Rest Assured 中的 JsonPath 使用 Groovy’s GPath notation,这不会跟 Jayway 的 JsonPath 语法混淆。总之,市面上有很多 JsonPath 的语法,今天我们主要了解 Rest Assured 中的支持的 JsonPath。

“$” 表示根结点,但在 RestAssured 脚本里,没有必要加这个根结点符号。父子关系结点可以用 (.) 或 [< childName >]。还可用用 [index] 表示数组中的元素。

JSON Object:

[{
    
    
  "id": 1,
  "first_name": "Lothaire",
  "last_name": "Benazet",
  "email": "[email protected]",
  "gender": "Male"
}, {
    
    
  "id": 2,
  "first_name": "Shellie",
  "last_name": "Cowser",
  "email": "[email protected]",
  "gender": "Female"
}, {
    
    
  "id": 3,
  "first_name": "Sharl",
  "last_name": "Hesbrook",
  "email": "[email protected]",
  "gender": "Female"
}, {
    
    
  "id": 4,
  "first_name": "Merrili",
  "last_name": "Acom",
  "email": "[email protected]",
  "gender": "Female"
}, {
    
    
  "id": 5,
  "first_name": "Remus",
  "last_name": "Downgate",
  "email": "[email protected]",
  "gender": "Male"
}, {
    
    
  "id": 6,
  "first_name": "Tatiana",
  "last_name": "Tribble",
  "email": "[email protected]",
  "gender": "Female"
}, {
    
    
  "id": 7,
  "first_name": "Wood",
  "last_name": "Hebbes",
  "email": "[email protected]",
  "gender": "Male"
}, {
    
    
  "id": 8,
  "first_name": "Kendall",
  "last_name": "Bony",
  "email": "[email protected]",
  "gender": "Male"
}, {
    
    
  "id": 9,
  "first_name": "Robinet",
  "last_name": "Gooday",
  "email": "[email protected]",
  "gender": "Male"
}, {
    
    
  "id": 10,
  "first_name": "Laural",
  "last_name": "Krzysztofiak",
  "email": "[email protected]",
  "gender": "Female"
}]

代码:

import java.io.File;
 
import io.restassured.path.json.JsonPath;
 
public class SyntaxForJsonArray {
    
    
	
	public static void main(String[] args) {
    
    
		
		File jsonArrayFile = new File(System.getProperty("user.dir")+"\\src\\main\\resources\\jsonFiles\\jsonArraySyntaxDemo.json");
		JsonPath jsonPath = JsonPath.from(jsonArrayFile);
		
		/*
		 * To get a specific field value of an indexed element [0] will give you first
		 * element in an array and using dot operator we are retrieving value of
		 * firstName
		 */
		System.out.println("First name is first employee :"+ jsonPath.getString("[0].first_name"));
		
		// To get whole indexed element
		System.out.println("All details of first employee : " +jsonPath.getJsonObject("[0]"));
		
		/*
		 * To get first names of all employees Since it is a JSON array and we are not
		 * specifying index then it will pick firstName from each element and return as
		 * a list.
		 */
		System.out.println("First name of all employees" + jsonPath.getList("first_name"));
		
		/* To get all first name of all females only 
		 * If we want to filter records based on conditions we can use find or findAll expression.
		 * findAll will iterate through each element in array and match condition. "it" represent current element.
		 * For each element it will check if gender is "female". If yes then take firstName of current element. 
		 * findAl returns a List. */
		System.out.println("First name of all female employees : "+jsonPath.getList("findAll{it.gender == 'Female'}.first_name"));
		System.out.println("First name of all female employees : "+jsonPath.getList("findAll{it -> it.gender == 'Female'}.first_name"));
		
		/* To get first female name 
		 * If we want to get firstName of first female employee we can use find expression.*/
		System.out.println("First name of first female employee : "+jsonPath.getString("find{it.gender == 'Female'}.first_name"));
		
		/*
		 * We can also use relational operator like first name of all employees whose id is 5 or more
		 */
		System.out.println("First name of all employees whose id is 5 or more : " + jsonPath.getList("findAll{it.id >= 5}.first_name"));
		
		// we can use use and (&) operator - logical
		System.out.println("First name of all employees whose id is 5 or more but less than 8 : " + jsonPath.getList("findAll{it.id >= 5 & it.id < 8}.first_name"));
		
		// We can also use or (|) operator
		System.out.println("First name of all employees whose id is greater than 9 or gender is female : " + jsonPath.getList("findAll{it.id >= 9 | it.gender == 'Female'}.first_name"));
		
		// We can get size of array using size() or size
		System.out.println("Total number of employees : " + jsonPath.getString("size()"));
		
		
	} 
}

输出:

First name is first employee :Lothaire
All details of first employee : {
    
    id=1, first_name=Lothaire, last_name=Benazet, email=lbenazet0@tinyurl.com, gender=Male}
First name of all employees[Lothaire, Shellie, Sharl, Merrili, Remus, Tatiana, Wood, Kendall, Robinet, Laural]
First name of all female employees : [Shellie, Sharl, Merrili, Tatiana, Laural]
First name of all female employees : [Shellie, Sharl, Merrili, Tatiana, Laural]
First name of first female employee : Shellie
First name of all employees whose id is 5 or more : [Remus, Tatiana, Wood, Kendall, Robinet, Laural]
First name of all employees whose id is 5 or more but less than 8 : [Remus, Tatiana, Wood]
First name of all employees whose id is greater than 9 or gender is female : [Shellie, Sharl, Merrili, Tatiana, Robinet, Laural]
Total number of employees : 10

Guess you like

Origin blog.csdn.net/wumingxiaoyao/article/details/120594419