REST Assured 系列汇总 之 REST Assured 59 - How To Create JsonPath For Simple And Nested JSON Array?
介绍
如果你对 XPath 有所了解 ,那么就更容易理解 JsonPath。区别在于 XPath 表示一个 XML 文档中到达一个 node 的路径,而 JsonPath 就表示一个 JSON 文档中到达一个 node 的路径。
本文我们将了解以下内容:
- 简单的 JSON array 的 JsonPath
- 嵌套的 JSON arrays 的 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 objects 的 JsonPath:
What Is JsonPath And How To Create It For Simple And Nested JSON Object?
简单的 JSON Array 的JsonPath
一个 JSON Array 包含一个 或 多个元素,元素之间用逗号分开,例如:
{
"firstName": "Amod",
"lastName": "Mahajan",
"address": [
{
"type": "permanent",
"city": "Bengaluru",
"state": "KA"
},
{
"type": "temp",
"city": "Bhopal",
"state": "MP"
}
]
}
上面这个JSON,“address” 结点是一个数组,由2个元素 或 JSON objects 组成。数组中的元素是用逗号分开的。任何一门语言,访问数组中的元素都是通过它的 index,index 从 0 开始。“address[0]” 表示 “address” 数组的第一个元素。相似地, “address[1]” 表示 “address” 数组的第二个元素。
一旦通过 index 获取到数组里的元素,我们可以通过 (.
) 来遍历它们的子元素。例如:
“address[0].type” 返回 “permanent”
“address[1].state” 返回 “MP”
“address” 返回数组所有的元素
我们也可以通过 JsonPath 的 getList() 方法来获取数组中改具体字段的所有值。
代码:
import java.util.List;
import java.util.Map;
import io.restassured.path.json.JsonPath;
public class SimpleJsonArray {
public static void main(String[] args) {
String jsonArrayString = "{\r\n" +
" \"firstName\": \"Amod\",\r\n" +
" \"lastName\": \"Mahajan\",\r\n" +
" \"address\": [\r\n" +
" {\r\n" +
" \"type\": \"permanent\",\r\n" +
" \"city\": \"Bengaluru\",\r\n" +
" \"state\": \"KA\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"type\": \"temp\",\r\n" +
" \"city\": \"Bhopal\",\r\n" +
" \"state\": \"MP\"\r\n" +
" }\r\n" +
" ]\r\n" +
"}";
//Get JsonPath instance of above JSON string
JsonPath jsonPath = JsonPath.from(jsonArrayString);
// Since address holds a JSON array we can get particular indexed element using index
String addressType1 = jsonPath.getString("address[0].type");
System.out.println("Address type is : "+addressType1);
String addressType2 = jsonPath.getString("address[1].type");
System.out.println("Another address type is : "+addressType2);
// We can get address types as a list as well
List<String> allAddressTypes = jsonPath.getList("address.type");
System.out.println(allAddressTypes);
// We can get complete address array as List
// Since it holds Json objects which can be a Map
List<Map<String,Object>> allAddress = jsonPath.getList("address");
for(Map<String,Object> address : allAddress)
{
System.out.println(address);
}
}
}
输出:
Address type is : permanent
Another address type is : temp
[permanent, temp]
{
type=permanent, city=Bengaluru, state=KA}
{
type=temp, city=Bhopal, state=MP}
嵌套 JSON Array 的 JsonPath
一个 JSON Array 里面嵌套一个 JSON Array 就称为嵌套的 JSON Arrays。
{
"firstName": "Amod",
"lastName": "Mahajan",
"address": [[
{
"type": "permanent",
"city": "Bengaluru",
"state": "KA"
},
{
"type": "temp",
"city": "Bhopal",
"state": "MP"
}
],
[
{
"type": "communication",
"city": "Delhi",
"state": "DL"
},
{
"type": "old",
"city": "Kanpur",
"state": "UP"
}
]]
}
代码:
import java.util.List;
import java.util.Map;
import io.restassured.path.json.JsonPath;
public class NestedJsonArray {
public static void main(String[] args) {
String jsonArrayString = "{\r\n" +
" \"firstName\": \"Amod\",\r\n" +
" \"lastName\": \"Mahajan\",\r\n" +
" \"address\": [\r\n" +
" [\r\n" +
" {\r\n" +
" \"type\": \"permanent\",\r\n" +
" \"city\": \"Bengaluru\",\r\n" +
" \"state\": \"KA\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"type\": \"temp\",\r\n" +
" \"city\": \"Bhopal\",\r\n" +
" \"state\": \"MP\"\r\n" +
" }\r\n" +
" ],\r\n" +
" [\r\n" +
" {\r\n" +
" \"type\": \"communication\",\r\n" +
" \"city\": \"Delhi\",\r\n" +
" \"state\": \"DL\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"type\": \"old\",\r\n" +
" \"city\": \"Kanpur\",\r\n" +
" \"state\": \"UP\"\r\n" +
" }\r\n" +
" ]\r\n" +
" ]\r\n" +
"}";
//Get JsonPath instance of above JSON string
JsonPath jsonPath = JsonPath.from(jsonArrayString);
// Since address holds nested JSON arrays we can get particular indexed element using index
// followed by another index
String addressType1 = jsonPath.getString("address[0][0].type");
System.out.println("Address type is : "+addressType1);
String addressType2 = jsonPath.getString("address[0][1].type");
System.out.println("Second address type is : "+addressType2);
String addressType3 = jsonPath.getString("address[1][0].type");
System.out.println("Third type is : "+addressType3);
String addressType4 = jsonPath.getString("address[1][1].type");
System.out.println("Fourth address type is : "+addressType4);
// We can get address types from first array of address
List<String> allAddressTypesOfFirstElementOfArray = jsonPath.getList("address[0].type");
System.out.println(allAddressTypesOfFirstElementOfArray);
// We can get address types from second array of address
List<String> allAddressTypesOfSecondElementOfArray = jsonPath.getList("address[1].type");
System.out.println(allAddressTypesOfSecondElementOfArray);
// We can get address types from all elements of array of address
List<String> allAddressTypes = jsonPath.getList("address.type");
System.out.println(allAddressTypes);
}
}
输出:
Address type is : permanent
Second address type is : temp
Third type is : communication
Fourth address type is : old
[permanent, temp]
[communication, old]
[[permanent, temp], [communication, old]]