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