解釋如何透過 Rest Assured 獲取 JSON 陣列響應的大小。
我們可以在 Rest Assured 中獲取 JSON 陣列響應的大小。首先,我們將從請求中獲取 JSON 格式的響應主體。然後將其轉換為字串。最後,使用 size 方法獲取其長度。程式碼實現
import static io.restassured.RestAssured.given; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.path.json.JsonPath; import io.restassured.response.Response; public class NewTest { @Test public void jsonAryLen() { //obtain Response from GET request Response res = given() .when() .get("https://jsonplaceholder.typicode.com/posts"); //convert JSON to string JsonPath j = new JsonPath(res.asString()); //length of JSON array int s = j.getInt("data.size()"); System.out.println( s); } }
輸出
廣告