REST Assured 系列汇总 之 REST Assured 61 - Deserialize Using JsonPath
介绍
早期我们有介绍 De-Serialization – JSON Object To Java Object Using Jackson API,同样我们也可以通过 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 反序列化Deserialization
We will take a very simple JSON Object and will create a POJO for it.
将拿一个简单的 JSON Object 为例子,为它创建一个 POJO 类。
JSON Object
{
"id": 1,
"first_name": "Lothaire",
"last_name": "Benazet",
"email": "[email protected]",
"gender": "Male"
}
Employee POJO
public class Employee {
private Integer id;
private String first_name;
private String last_name;
private String email;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
将一个 Java object 转换成一个 JSON Object 称为序列化 serialization, 反之是反序列化 Deserialization。JsonPath 类提供一个方法 “T getObject(String path, Class objectType)” 获取一个 Object 的 path 的结果作为一个 Java Object。
反序列化
import io.restassured.path.json.JsonPath;
public class DeserializationUsingJsonPath {
public static void main(String[] args) {
String jsonObject = "{\r\n" +
" \"id\": 1,\r\n" +
" \"first_name\": \"Lothaire\",\r\n" +
" \"last_name\": \"Benazet\",\r\n" +
" \"email\": \"[email protected]\",\r\n" +
" \"gender\": \"Male\"\r\n" +
"}";
JsonPath jsonPath = JsonPath.from(jsonObject);
Employee employee = jsonPath.getObject("", Employee.class);
System.out.println("Id is : "+employee.getId());
System.out.println("First name is : "+employee.getFirst_name());
System.out.println("Last name is : "+employee.getLast_name());
System.out.println("Email id is : "+employee.getEmail());
System.out.println("Gender is : "+employee.getGender());
// Another way
Employee employee1 = jsonPath.getObject("$", Employee.class);
System.out.println("Id is : "+employee1.getId());
System.out.println("First name is : "+employee1.getFirst_name());
System.out.println("Last name is : "+employee1.getLast_name());
System.out.println("Email id is : "+employee1.getEmail());
System.out.println("Gender is : "+employee1.getGender());
}
}
输出:
Id is : 1
First name is : Lothaire
Last name is : Benazet
Email id is : lbenazet0@tinyurl.com
Gender is : Male
Id is : 1
First name is : Lothaire
Last name is : Benazet
Email id is : lbenazet0@tinyurl.com
Gender is : Male
JSON Array
上面例子用 “” 和 “ $ ” 作为 JSON path,可以得到一个简单的 JSON Object。如果是如下一个嵌套的 JSON Array,我们需要写一个适当的 JSON Path 来反序列化结果。
[{
"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"
}]
如果我们只想反序列化上面 JSON Array 中第二个 JSON Object,实现代码如下:
import io.restassured.path.json.JsonPath;
public class DeserializationUsingJsonPath {
public static void main(String[] args) {
String jsonObject = "[{\r\n" +
" \"id\": 1,\r\n" +
" \"first_name\": \"Lothaire\",\r\n" +
" \"last_name\": \"Benazet\",\r\n" +
" \"email\": \"[email protected]\",\r\n" +
" \"gender\": \"Male\"\r\n" +
"}, {\r\n" +
" \"id\": 2,\r\n" +
" \"first_name\": \"Shellie\",\r\n" +
" \"last_name\": \"Cowser\",\r\n" +
" \"email\": \"[email protected]\",\r\n" +
" \"gender\": \"Female\"\r\n" +
"}]";
JsonPath jsonPath = JsonPath.from(jsonObject);
Employee employee = jsonPath.getObject("[1]", Employee.class);
System.out.println("Id is : "+employee.getId());
System.out.println("First name is : "+employee.getFirst_name());
System.out.println("Last name is : "+employee.getLast_name());
System.out.println("Email id is : "+employee.getEmail());
System.out.println("Gender is : "+employee.getGender());
}
}
输出:
Id is : 2
First name is : Shellie
Last name is : Cowser
Email id is : scowser1@163.com
Gender is : Female