我如何从json数组中提取数据,以便在后续的rest保证api请求中使用



我正在使用restassured with Java来自动化API,并希望从GET响应中提取idrecommendIdproductId,以传递到另一个post请求中,还想知道如何从数组中的第二个对象中提取数据。

到目前为止,我一直在努力通过搜索来实现这一点,并感谢您在如何处理这一问题上提供的帮助。

{
"result":"OK",
"reason":null,
"content":[
{
"id":"00AX",
"publishingDate":"2018-05-29",
"expiryDate":"2018-11-18",
"title":"Terry William",
"agentName":"Faith Davis",
"status":"Viewed",
"products":[
{
"recommendId":"003",
"code":"9765455",
"brand":"Gucci",
"category":"COATS",
"description":"Trouser",
"itemStatus":"In Stock",
"siteStatus":"Approved",
"reservedStatus":"Recommended",
"reservedSize":"10 UK",
"productIDs":[
{
"productId":"23",
"size":"4 UK"
},
{
"productId":"32",
"size":"6 UK"
},
{
"productId":"12",
"size":"8 UK"
},
{
"productId":"25",
"size":"10 UK"
},
{
"productId":"39",
"size":"12 UK"
},
{
"productId":"76",
"size":"14 UK"
}
],
"url":"https://image.jpg"
}
]
},
{
"id":"00AP",
"publishingDate":"2018-05-29",
"expiryDate":"2018-11-18",
"title":"Jones Bob",
"agentName":"Mary Jones",
"status":"Viewed",
"products":[
{
"recommendId":"002",
"code":"6767464",
"brand":"LV",
"category":"BAGS",
"description":"Loewe Bags",
"itemStatus":"In Stock",
"siteStatus":"Approved",
"reservedStatus":"Recommended",
"reservedSize":"10 UK",
"productIDs":[
{
"productId":"23",
"size":"4 UK"
},
{
"productId":"32",
"size":"6 UK"
},
{
"productId":"12",
"size":"8 UK"
},
{
"productId":"25",
"size":"10 UK"
},
{
"productId":"39",
"size":"12 UK"
},
{
"productId":"76",
"size":"14 UK"
}
],
"url":"https://image.jpg"
}
]
}
]
}

试试这个,

// Base Test [BaseTest.java]
public class BaseTest {
protected RequestSpecification requestSpecificationToMerge = new RequestSpecBuilder()
.setBaseUri("Your Base Url")
.setContentType(ContentType.JSON)
.build();
@BeforeMethod
public void setFilter() {
RestAssured.filters(new AllureRestAssured());
}
}
// Return Id,recommendId and productId [Function.java]
public class ListCompetition extends BaseTest {
public String returnId() {
return given()
.spec(requestSpecificationToMerge)
.basePath("/your endpoint")
.when()
.get()
.getBody()
.path("content[0].id").toString();
}
public String recommentId() {
return given()
.spec(requestSpecificationToMerge)
.basePath("/your endpoint")
.when()
.get()
.getBody()
.path("content[0].products[0].recommendId").toString();
}
public String productId() {
return given()
.spec(requestSpecificationToMerge)
.basePath("/your endpoint")
.when()
.get()
.getBody()
.path("content[0].products[0].productIDs[0].productId").toString();
}
}

您可以为测试所需的ID、RECOMMENTID和PRODUCTID 调用此函数

相关内容

  • 没有找到相关文章

最新更新