簡體   English   中英

如何使用 Rest Assured 在響應 API 中獲取兩個具有相同名稱的不同字段?

[英]How to get two different fields with the same name in a Response API using Rest Assured?

我正在嘗試使用 Rest-Assured 和 Java 為 GET API 創建測試自動化。

此 API 具有以下響應正文:

{
    "items": [
        {
            "id": "3185",
            "customer_id": "299",
            "region": "São Paulo",
            "region_id": 1234,
            "country_id": "BR",
            "street": [
                "Av Paulista"
            ],
            "company": "Teste",
            "telephone": "(19) 99999-9999",
            "postcode": "",
            "city": "Valinhos",
            "firstname": "N/A",
            "lastname": "N/A",
            "middlename": null,
            "prefix": null,
            "suffix": null,
            "person_type": "PF",
            "document": "43448871703",
            "state_registry": null,
            "created_at": "2019-07-24 13:03:29"
        },
        {
            "id": "3188",
            "customer_id": "299",
            "region": "São Paulo",
            "region_id": 1234,
            "country_id": "BR",
            "street": [
                "Av Paulista"
            ],
            "company": "Teste",
            "telephone": "(19) 99999-9999",
            "postcode": "",
            "city": "Valinhos",
            "firstname": "N/A",
            "lastname": "N/A",
            "middlename": null,
            "prefix": null,
            "suffix": null,
            "person_type": "PJ",
            "document": "84047942000115",
            "state_registry": null,
            "created_at": "2019-07-24 13:03:30"
        }
    ]
}

在此 API 響應中,有兩個具有相同名稱“id”的字段。 如何獲取這兩個字段的值?

謝謝

看看這篇文章: https : //techeplanet.com/parse-json-array-using-rest-assured/

    @Test
    public void verifyJSONArrayResponse() {
        JsonArray jsonArray = new JsonArray();

        jsonArray = given().baseUri("http://<your host>")
                .basePath("<your path>")
                .get().as(JsonArray.class);

        for (int i = 0; i < jsonArray.size(); i++) {
            JsonObject jsonObject = jsonArray.get(i).getAsJsonObject();
            System.out.println(jsonObject.get("id").getAsString());
        }
    }

不過,您需要稍微調整一下,以首先從頂級響應對象中提取items (您的數組)。

您可以使用 JsonPath 輕松完成此操作: $.items[*].id這將為您提供兩個 ID。

由於您使用的是 REST-assured,您可以直接從響應中提取您想要的內容,如下所示:

List<Integer> = given()
                  .spec(yourRequestSpecification)
                  .get("/your_api_endpoint") // returns Response
                  .then()                    // returns ValidatableResponse
                  .extract()                 // returns ExtractableResponse
                  .path("items.id");         // Groovy GPath syntax

Jayway 的 jsonpathREST-assured 的 jsonpath使用不同的語法

你可以試試下面的代碼。

import org.json.JSONArray;
import org.json.JSONException;
import org.testng.annotations.Test;

import bsh.ParseException;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class KeyValueJsonArrayExample {

    @Test
    public void getResponseAsJsonArray() throws ParseException, JSONException {

        RequestSpecification myreq = RestAssured.given();

        Response MyRes;
        MyRes = myreq.get("http://localhost:3000/posts/");
        String jsonResponse = MyRes.asString();
        System.out.println("Full String is" + jsonResponse);

        JSONArray jsonArray = new JSONArray(jsonResponse);
        for (int i = 0; i < jsonArray.length(); i++) {
            org.json.JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            String value1 = jsonObject1.optString("id");
            String value2 = jsonObject1.optString("auhtor");
            String value3 = jsonObject1.optString("title");
            System.out.println("The values are" + value1 + value2 + value3);
        }

    }

}

另外,如果您只需要獲取特定索引,請嘗試

Response response=given().contentType(ContentType.JSON).get("http://localhost:3000/posts");
        JsonPath jsonPathEvaluator = response.jsonPath();
        String title = jsonPathEvaluator.get("title[2]");
        String author=jsonPathEvaluator.get("author[2]");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM