首页 > 解决方案 > 获取 ids 数组,但需要获取 String

问题描述

我正在放心地测试 API,并尝试使用以下方法从我创建的产品中获取 Id 作为字符串

   String productId = response.jsonPath().getString("productPrices.productId");
   String channelId = response.jsonPath().getString("productPrices.channelId");

但它返回给我一个数组

当我打印出这些 ID 时,将得到以下输出;

 System.out.println("productId is ===> " +productId);
System.out.println("channelId is ===> " +channelId);

输出是

productId is ===>[jpjeneOM]
channelId is ===>[5bpJKaVE]

我的问题是,我只需要将这些 ID 作为字符串获取!请帮我解决这个问题提前谢谢

我有以下有效载荷

{
    "id": "jpjeneOM",
    "name": "NewProductForTest",
    "productTypeId": null,
    "active": true,
    "shortDescription": "API",
    "fullDescription": "API Automation",
    "policy": null,
    "mostPopular": null,
    "longitude": 34.774667,
    "latitude": 32.085814,
    "calendarColor": "blue",
    "address": "Dizengoff Street, Tel Aviv-Yafo",
    "cityId": "nlYxZ6az",
    "createdAt": "2021-08-06T07:49:14.920615",
    "productImages": [
        
    ],
    "productDocuments": [
        
    ],
    "productTypes": null,
    "productWorkingDates": [
        {
            "id": "baQkKROA",
            "productId": "jpjeneOM",
            "fromDate": "2021-05-27",
            "toDate": "2022-12-31",
            "name": "StrickHours",
            "strictHours": true,
            "timeSlots": [
                {
                    "id": "1lkvdZa6",
                    "productWorkingDateId": "baQkKROA",
                    "dayOfWeek": "Monday",
                    "startTime": "09:00:00",
                    "endTime": "20:00:00",
                    "duration": "01:00:00",
                    "quantity": 67,
                    "usedQuantity": 0,
                    "active": true,
                    "deletedAt": null
                }
            ],
            "deletedAt": null,
            "maxUsedTicketsQuantity": 0,
            "errorCode": 0
        }
    ],
    "productRegions": [
        {
            "id": "4lrWZgOd",
            "productId": "jpjeneOM",
            "regionId": "5bpJKaVE",
            "regionName": null
        }
    ],
    "productPrices": [
        {
            "id": "1lon0Aa7",
            "productId": "jpjeneOM",
            "channelId": "5bpJKaVE",
            "fromDate": "2021-08-02T00:00:00",
            "toDate": "2022-12-31T00:00:00",
            "title": "API",
            "remarks": "Test",
            "officialPrice": 34,
            "currencyId": "5bpJKaVE",
            "quantityPerDay": 123,
            "autoConfirm": true,
            "pricePerPerson": false,
            "productPriceTypes": [
                {
                    "id": "xl4WJ7aj",
                    "productPriceId": "1lon0Aa7",
                    "productCustomerTypeId": "ZOw36ZOx",
                    "amount": 12,
                    "currencyId": "5bpJKaVE",
                    "currencySymbol": null,
                    "isOfficial": false,
                    "productCustomerType": null
                }
            ],
            "deletedAt": null
        }
    ]

标签: javaautomated-testscucumberrest-assured

解决方案


in的productPrices例子是一个数组,你没有提到选择哪一个,jsonpath 的语法是 like productPrices.*.channelId,所以你得到了数组。你可以选择第一个like productPrices.0.channelIdor else。


推荐阅读