【问题标题】:getting array of ids , but need to get String获取 ids 数组,但需要获取 String
【发布时间】:2021-08-06 08:12:50
【问题描述】:

我正在放心地测试 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
        }
    ]

【问题讨论】:

    标签: java automated-tests cucumber rest-assured


    【解决方案1】:

    示例中的productPrices 是一个数组,你没有提到选择哪一个,jsonpath 语法就像productPrices.*.channelId,所以你得到了数组。您可以选择第一个,如productPrices.0.channelId 或其他。

    【讨论】:

    • 非常感谢您的回复,我按照您的建议做了同样的事情 String productId = commonData.response.jsonPath().getString("productPrices.0.productId"); String channelId = commonData.response.jsonPath().getString("productPrices.0.channelId"); 输出为空数组; productId 是 ===> [] channelId 是 ===> []
    • 你使用的是哪个 json 路径解析器?
    • 非常感谢您在您的帮助下给我一个想法,productPrices.productId[0], productPrices.channelId[0]
    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多