gpt4 book ai didi

java - 从包含多个项目的 Rest Assured Json 响应中获取随机字段

转载 作者:行者123 更新时间:2023-12-01 22:27:14 27 4
gpt4 key购买 nike

从 Ruby 迁移到 Java,我需要从多个项目响应中解析并随机获取一个字段。

这是我用来获取响应的 ApiCall 方法:

        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
}

问题是我需要获得一个随机的“标题”字段,该字段在响应中的总数中至少有一个“children_uuids”。

据我了解,步骤是:

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

这给了我以下错误:

io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document

作为引用,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 中也是如此。

我可以建议你看一下Karate (免责声明: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'] }

关于java - 从包含多个项目的 Rest Assured Json 响应中获取随机字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42961124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com