如何使用Rest Assured在Response API中获得两个具有相同名称的不同字段?

时间:2019-07-26 10:51:14

标签: java rest-assured rest-assured-jsonpath

我正在尝试使用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”的字段。如何获得这两个字段的值?

谢谢

4 个答案:

答案 0 :(得分:1)

看看这篇文章: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(数组)。

答案 1 :(得分:0)

您可以使用JsonPath轻松做到这一点:$.items[*].id这将为您提供两个ID。

答案 2 :(得分:0)

由于您使用的是REST保证的,因此可以直接从响应本身中提取所需内容,如下所示:

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

Jayway's jsonpathREST-assured's jsonpath使用不同的语法

答案 3 :(得分:0)

您可以尝试以下代码。

frames = [test.loc[:, 'ID'],
          test.loc[:, test.columns.str.startswith('rfm')],
          test.loc[:, 'a':'c'],
          test.iloc[:, -1]]

test_sub = pd.concat(frames, axis=1)

如果您只需要获取特定的索引,请尝试

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);
        }

    }

}
相关问题