Java 从多个项目的Rest Assured Json响应中获取随机场
从Ruby迁移到Java,我需要从一个多项目响应中随机解析并获取一个字段 这是我用来获得回复的ApiCall方法:Java 从多个项目的Rest Assured Json响应中获取随机场,java,json,rest-assured,Java,Json,Rest Assured,从Ruby迁移到Java,我需要从一个多项目响应中随机解析并获取一个字段 这是我用来获得回复的ApiCall方法: Response response = given(). headers(this.headers). params(this.body). when(). post(this.url). then(). contentType(Conte
Response response = given().
headers(this.headers).
params(this.body).
when().
post(this.url).
then().
contentType(ContentType.JSON).
statusCode(200).
extract().
response();
它给了我这个JSON结构:
{
"Contents": {
"Title": "Search results",
"Count": "10",
"Page": "1",
"TotalCount": "1",
"TotalPages": 2,
"Genres": [
"Genre_1",
"Genre_2",
"Genre_3"
],
"Contents": [
{
"title": "content1_title",
"original_text": "original text 1",
"full_text": "Sample full sized text 1",
"short_text": "Sample short text 1",
"children_ids": {
"item": [
1,
2
]
},
"children_uuids": {
"item": [
"item1_uuid",
"item2_uuid"
]
},
"parent_ids": {
"item": [
1
]
},
"parent_uuids": {
"item": [
"item1_uuid"
]
},
"aired_from": "1994-01-01",
"aired_to": "1994-12-31",
"tags": {
"item": [
""
]
},
"available_condition1": 0,
"available_condition2": 1,
"price_condition1": "0.00",
"price_condition2": "13.00"
},
{
"title": "content2_title",
"original_text": "original text 2",
"full_text": "Sample full sized text 2",
"short_text": "Sample short text 2",
"children_ids": {
"item": [
1,
2
]
},
"children_uuids": {
"item": [
"item1_uuid",
"item2_uuid"
]
},
"parent_ids": {
"item": [
1
]
},
"parent_uuids": {
"item": [
"item1_uuid"
]
},
"aired_from": "1998-01-01",
"aired_to": "1998-01-31",
"tags": {
"item": [
""
]
},
"available_condition1": 0,
"available_condition2": 1,
"price_condition1": "0.00",
"price_condition2": "13.00"
}
]
},
"Success": true
}
问题是我需要得到一个随机的“title”字段,其中至少有一个“children_uuid”来自于响应中的“children_uuid”
据我所知,这些步骤是:
1) 获取“Contents.Contents”项的总大小。
2) 获取一个介于0和项目总数之间的随机数。
3) 使用该数字选择一个“Contents.Contents.[n].title”格式或类似格式的项目
尝试了以下操作但未成功:
JsonPath jsonPath = new JsonPath(response.toString());
List<?> list = jsonPath.getList("Contents.Contents.title.flatten()");
System.out.println(list);
作为参考,Ruby中的代码如下:
amount = @result['api_response']['Contents']['Contents'].count
amount = amount - 1
a = rand(0..amount)
while @result['api_response']['Contents']['Contents'][a]['children_uuids']['item'].nil? do
a = rand(0..amount)
#children_uuids = $result['api_response']['Contents']['Contents'][a]['children_uuids']
end
#price_hd = @result['api_response']['Contents']['Contents'][a]['price_hd']
content_title = @result['api_response']['Contents']['Contents'][a]['title']
更新:我已经让它部分工作。。。我找到了一种从列表中选择一项的方法,该行为:
String contentTitle = response.then().extract().path("Contents.Contents[0].title");
但是找不到使用此jsonpath的方法
String contentTitle = response.then().extract().path("Contents.Contents.[?(@.children_uuids)].uuid");
第二行告诉我:
java.lang.IllegalArgumentException:
Invalid JSON expression:
Script1.groovy: 1: unexpected token: [ @ line 1, column 45.
nRootObject.Contents.Contents.[?(@.child
^
提前感谢。在我看来,这很难在REST-assured中实现,甚至在Java中也很难做到 我建议你看一下(免责声明:am-dev)。因此,您可以调用JavaScript函数来生成一个随机数,将其分配给一个变量,然后形成一个路径表达式来完成您想要的操作:
Feature:
Scenario:
* def data = read('data.json')
* def size = (data.Contents.Contents.length)
* def index = Math.floor(Math.random() * size)
* print 'selected index: ' + index
* def item = (data.Contents.Contents[index])
* def children_uuids = item.children_uuids
* match children_uuids == { item: ['item1_uuid', 'item2_uuid'] }
更新:我找到了具有以下代码的解决方案:
// get random content data
Integer contentAmmount = response.body().path("Contents.Contents.size()");
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(contentAmmount);
while (response.body().path(
String.join("", "Contents.Contents[", Integer.toString(randomInt),
"].children_uuids.item")).toString().isEmpty() ) {
randomInt = randomGenerator.nextInt(contentAmmount);
}
String contentTitle = response.then().extract()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].title"));
String contentUuid = response.then().extract()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].uuid"));
String contentChildrenUuid = response.body().path(
String.join("", "Contents.Contents[", Integer.toString(randomInt),
"].children_uuids.item"));
// get random content data
Integer contentAmmount = response.body().path("Contents.Contents.size()");
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(contentAmmount);
while (response.body().path(
String.join("", "Contents.Contents[", Integer.toString(randomInt),
"].children_uuids.item")).toString().isEmpty() ) {
randomInt = randomGenerator.nextInt(contentAmmount);
}
String contentTitle = response.then().extract()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].title"));
String contentUuid = response.then().extract()
.path(String.join("", "Contents.Contents[", Integer.toString(randomInt), "].uuid"));
String contentChildrenUuid = response.body().path(
String.join("", "Contents.Contents[", Integer.toString(randomInt),
"].children_uuids.item"));