该函数包含 API,我可以在其中获取 JSON 格式的值。但我想获得 JSON 中存在的确切 ID=2 如何使用 Java JSON 路径获取该数据。我必须使用 Maven 依赖项。该函数包含 API,我可以在其中获取 JSON 格式的值。但我想获得 JSON 中存在的确切 ID=2 如何使用 Java JSON 路径获取该数据。我必须使用 Maven 依赖项。
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
void get_extract_response_body() {
//Specify Base URL
RestAssured.baseURI="https://reqres.in/api";
//Request object
RequestSpecification httpRequest=RestAssured.given();
//Response Object
Response response=httpRequest.request(Method.GET,"/unknown");
String responseBody= response.getBody().asString();
System.out.println(responseBody);
//Print Response in console window
JsonPath jsonpath= response.jsonPath();
System.out.println(jsonpath.get("data[0]."));
/*
* String id=jsonpath.get("$.data[1].id"); System.out.println(id);
*/
}
Run Code Online (Sandbox Code Playgroud)
{
"page": 1,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 1,
"name": …Run Code Online (Sandbox Code Playgroud)