java - 如何使用 response.path 计算响应(Json)的总和?
问题描述
似乎很难通过针对我的 Json 响应执行我放心的逻辑来计算具有“PLAYER”角色类型的小队成员的数量。
目前我有一个简单的设置,它到达一个端点,然后执行我的查询,我可以看到端点和响应是有效的,但是在尝试通过我的查询过滤以计算小队成员的总和时似乎存在问题扮演“PLAYER”角色的人。
我放心的代码:
@Test
public void locatePlayerCalculateSum() {
Response response = given()
.spec(footballCompetitions_requestSpecification)
.when().get(EndPoint.TEAMS + EndPoint.SQUAD);
int sum = response.path("squad.collect { it.role == \"PLAYER\" }.sum()");
System.out.println(sum);
}
异常消息:java.lang.IllegalArgumentException:没有方法签名:java.lang.Boolean.plus() 适用于参数类型:(java.lang.Boolean) 值:[true] 可能的解决方案:is(java.lang.对象),或(java.lang.Boolean),暗示(java.lang.Boolean),和(java.lang.Boolean),使用([Ljava.lang.Object;),拆分(groovy.lang.Closure)
示例 JSON 响应:
"id": 66,
"area": {
"id": 2072,
"name": "England"
},
"activeCompetitions": [
{
"id": 2021,
"area": {
"id": 2072,
"name": "England"
},
"name": "Premier League",
"code": "PL",
"plan": "TIER_ONE",
"lastUpdated": "2019-01-03T23:39:45Z"
},
{
"id": 2001,
"area": {
"id": 2077,
"name": "Europe"
},
"name": "UEFA Champions League",
"code": "CL",
"plan": "TIER_ONE",
"lastUpdated": "2018-12-13T18:55:02Z"
}
],
"name": "Manchester United FC",
"shortName": "Man United",
"tla": "MNU",
"crestUrl": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg",
"address": "Sir Matt Busby Way Manchester M16 0RA",
"phone": "+44 (0161) 8688000",
"website": "http://www.manutd.com",
"email": "enquiries@manutd.co.uk",
"founded": 1878,
"clubColors": "Red / White",
"venue": "Old Trafford",
"squad": [
{
"id": 3188,
"name": "David De Gea",
"position": "Goalkeeper",
"dateOfBirth": "1990-11-07T00:00:00Z",
"countryOfBirth": "Spain",
"nationality": "Spain",
"shirtNumber": 1,
"role": "PLAYER"
},
{
"id": 3202,
"name": "Sergio Romero",
"position": "Goalkeeper",
"dateOfBirth": "1987-02-22T00:00:00Z",
"countryOfBirth": "Argentina",
"nationality": "Argentina",
"shirtNumber": null,
"role": "PLAYER"
},
{
"id": 7942,
"name": "Lee Grant",
"position": "Goalkeeper",
"dateOfBirth": "1983-01-27T00:00:00Z",
"countryOfBirth": "England",
"nationality": "England",
"shirtNumber": 13,
"role": "PLAYER"
},
{
"id": 3206,
"name": "Marcos Rojo",
"position": "Defender",
"dateOfBirth": "1990-03-20T00:00:00Z",
"countryOfBirth": "Argentina",
"nationality": "Argentina",
"shirtNumber": 16,
"role": "PLAYER"
},```
解决方案
这应该有效:
@Test
public void locatePlayerCalculateSum() {
Response response = given()
.spec(footballCompetitions_requestSpecification)
.when().get(EndPoint.TEAMS + EndPoint.SQUAD);
int sum = response.path("squad.count { it.role == 'PLAYER' }");
System.out.println(sum);
}
推荐阅读
- openlayers - 如何通过无限平移(OpenLayers)获取投影特征的坐标
- credentials - 使用本地管理员凭据在服务器之间复制项目
- javascript - Javascript Image 对象未绘制到 2D 画布
- wordpress - 从WordPress中的插件实例化一个类
- r - 为树模型确定 R 中特定结果的概率
- c++ - 为什么使用 _access 时只读测试对读写文件有效?
- sql-server - ADServiceAccount 作为 SQL Server 代理的代理帐户 从 SSISDB 计划 SSIS 包
- sql - 基于另一个分组的条件分组
- .net - 是否可以为一种特定方法覆盖 SOAP 操作名称空间?
- download - 文件在 Chrome 中下载,但不在 Safari 中,但 console.log(response) 和 Network 选项卡显示从服务器发送的文件