REST Assured 58 - What Is JsonPath And How To Create It For Simple And Nested JSON Object?

REST Assured 系列汇总 之 REST Assured 58 - What Is JsonPath And How To Create It For Simple And Nested JSON Object?

介绍

如果你对 XPath 有所了解 ,那么就更容易理解 JsonPath。区别在于 XPath 表示一个 XML 文档中到达一个 node 的路径,而 JsonPath 就表示一个 JSON 文档中到达一个 node 的路径。

本文我们将了解以下内容:

  1. 什么是 JsonPath?
  2. 简单的 JSON object 的 JsonPath
  3. 嵌套的 JSON object 的 JsonPath

前提条件

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

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

什么是JsonPath?

JSON 代表 “JavaScript Object Notation” 是一门轻量级,语言无关性,自我描述的文本格式,用于数据存储和数据交换的标记符。JSON 更易人类读写,比 XML 更受欢迎。有关 JSON 更多信息,请参考之前的文章 JSON 介绍

一个 JSON 是由 JSON Nodes 或 JSON 元素组成。可以通过 path 来遍历一个 JSON 元素。

好比一个邮政地址 “上海市宝山区上大路1111号11单元110室” 按照这个地址就能找到某人的家一样,类似 JsonPath 也是一个路径,用来找到定位到某一个 node。

简单 JSON object 的 JsonPath

简单的 JSON object

{
    
    
  "firstName": "Kevin",
  "lastName": "Zhang"
} 

上面这个 JSON object,有两个 JSON 元素 或 “firstName” 和 “lastName” 节点封闭在花括号内。为了达到 “firstName” 节点,我们需要从第一个花括号开始。实际上,在任何 JSON object 里,都有一个根结点,用 “$” 来表示。从这个根结点开始,我们可以遍历到任何一个结点。

因为有一个根结点,所以 “firstName” 和 “lastName” 就是子结点。为了要引用一个父结点的子结点我们要用到 (.) 符号。

“firstName” 结点的 JsonPath: $.firstName
“lastName” 结点的 JsonPath: $.lastName

在 Rest Assured 脚本里写上面的 JsonPath,可以不用这个符 ($) 符号,因为 JsonPath 都有一个根结点。

扫描二维码关注公众号,回复: 13437141 查看本文章

为了要使用 RestAssured 的 JsonPath,需要得到 JSON object 的一个 JsonPath 实例。这样就可以通过不同方法来获取 JSON 元素的。

代码:

import io.restassured.path.json.JsonPath;
 
public class SimpleJsonObject {
    
    
	
	public static void main(String[] args) {
    
    
		
		String jsonString = "{\r\n" + 
				"  \"firstName\": \"Kevin\",\r\n" + 
				"  \"lastName\": \"Zhang\"\r\n" + 
				"}";
		
		//Get JsonPath instance of above JSON string
		JsonPath jsonPath = JsonPath.from(jsonString);
		
		// Since firstName holds a string value use getString() method and provide json path of firstName
		String firstName = jsonPath.getString("firstName");
		String lastName = jsonPath.getString("lastName");
		
		System.out.println("First name is : "+firstName);
		System.out.println("Last name is : "+lastName);
		
		// Since $ is root node of a JSON, we can get whole JSON string using $ as JSON path
		System.out.println(jsonPath.getString("$"));
		// There are two other ways to do the same thing as above
		System.out.println(jsonPath.getString(""));
		System.out.println(jsonPath.get());
		
	}
 
}

输出:

First name is : Kevin
Last name is : Zhang
[firstName:Kevin, lastName:Zhang]
[firstName:Kevin, lastName:Zhang]
{
    
    firstName=Kevin, lastName=Zhang}

嵌套JSON Object 的JsonPath

嵌套的 JSON Object 如下:

{
    
    
  "firstName": "Amod",
  "lastName": "Mahajan",
  "address": {
    
    
    "houseNo": 404,
    "buildingName": "Not Found",
    "streetName": "Gumnam gali",
    "city": "Bengaluru",
    "state": "Karnataka",
    "country": "India"
  },
  "skills": {
    
    
    "language": {
    
    
      "name": "Java",
      "proficiency": "Medium"
    }
  }
}

“houseNo” 结点的 path:
root node ($) -> address -> houseNo

最终的 JsonPath:
$.address.houseNo

相似地,“name” 结点的 JsonPath:
$.skills.language.name

代码:

import io.restassured.path.json.JsonPath;
 
public class NestedJsonObject {
    
    
	
	public static void main(String[] args) {
    
    
		
		String jsonString = "{\r\n" + 
				"  \"firstName\": \"Amod\",\r\n" + 
				"  \"lastName\": \"Mahajan\",\r\n" + 
				"  \"address\": {\r\n" + 
				"    \"houseNo\": 404,\r\n" + 
				"    \"buildingName\": \"Not Found\",\r\n" + 
				"    \"streetName\": \"Gumnam gali\",\r\n" + 
				"    \"city\": \"Bengaluru\",\r\n" + 
				"    \"state\": \"Karnataka\",\r\n" + 
				"    \"country\": \"India\"\r\n" + 
				"  },\r\n" + 
				"  \"skills\": {\r\n" + 
				"    \"language\": {\r\n" + 
				"      \"name\": \"Java\",\r\n" + 
				"      \"proficiency\": \"Medium\"\r\n" + 
				"    }\r\n" + 
				"  }\r\n" + 
				"}";
		
		//Get JsonPath instance of above JSON string
		JsonPath jsonPath = JsonPath.from(jsonString);
		
		// Since houseNo holds an int value use getInt() method and provide json path of houseNo
		int houseNo = jsonPath.getInt("address.houseNo");
		System.out.println("House no is : "+houseNo);
		
		String name = jsonPath.getString("skills.language.name");
		System.out.println("Name is : "+name);
		
	}
 
}

输出:

House no is : 404
Name is : Java

猜你喜欢

转载自blog.csdn.net/wumingxiaoyao/article/details/120592234