Rest Assured 中的 JSON 解析是什么?
我们能使用 Rest Assured 解析 JSON 响应。要解析一个 JSON 正文,我们应当使用 JSONPath 类,并利用这个类的函数来获取特定属性的值。
我们应当先通过 Postman 在一个模拟的 API URL 上发送一个 GET 请求,并观察响应正文。

示例
代码实现
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.response.ResponseBody;
import io.restassured.specification.RequestSpecification;
public class NewTest {
@Test
void responseParse() {
//base URI with Rest Assured class
RestAssured.baseURI = "https://run.mocky.io/v3";
//input details
RequestSpecification h = RestAssured.given();
//get response
Response res = h.get("/a1b7b64c-0204-409a-aa0c-e8314a5ddabf");
//Response body
ResponseBody b = res.getBody();
//convert response body to string
String responseBody = b.asString();
//JSON Representation from Response Body
JsonPath jsnPath = res.jsonPath();
//Get value of Location Key
String s = jsnPath.get("student");
System.out.println("Course name: " + s);
String std = jsnPath.get("standard");
System.out.println("Standard: " + std);
String m = jsnPath.get("marks");
System.out.println("Marks: " + m);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP