rest-assured - 如何绕过错误并解析 json 字段值
问题描述
如何在 JSON 以下反序列化?
当我使用下面的代码 Response response=engine.getResponse(Method.GET,strURL,strBAUsername,strBAPassword,null,ContentType.JSON); userEdit=response.as(TestUserRoleInfoList.class); // TestUserRoleInfoList 是一个 POJO 类
@JsonIgnoreProperties(ignoreUnknown=true) 公共类 TestUserRoleInfoList {
@JsonProperty("userRoleInfoList")
List<UserRoleInfo> userRoleInfo=new ArrayList<UserRoleInfo>();
public List<UserRoleInfo> getUserRoleInfo() {
return userRoleInfo;
}
public void setUserRoleInfo(List<UserRoleInfo> userRoleInfo) {
this.userRoleInfo = userRoleInfo;
}
}
不知何故,它无法解析 Regions 内部类。但我最不担心。我需要 collectionRole 字段。
如何获取集合角色列表?
{
"id": 5595,
"userName": "abc",
"firstName": "abc",
"lastName": "ijk",
"additionalName": "Ernest",
"namePrefix": {
"id": null,
"prefix": null,
"createdAt": null,
"updatedAt": null
},
"nameSuffix": {
"id": null,
"suffix": null,
"createdAt": null,
"updatedAt": null
},
"emplId": "11111",
"workDayId": "11111",
"staffEmailAddress": null,
"facultyEmailAddress": "abc.xyz@xxxx.edu",
"userRoleInfoList": [
{
"userId": 5595,
"collectionRole": "program-chair",
"regions": [
{
"id": 1,
"region": "Germany 1",
"regionalDirectorFirstName": "aaaa",
"regionalDirectorLastName": "bbbb",
"associateDirectorFirstName": null,
"associateDirectorLastName": null,
"division": {
"id": 2,
"name": "Europe",
"deletedFlag": false,
"createdAt": null,
"updatedAt": null
},
"deletedFlag": false,
"createdAt": null,
"updatedAt": null
}
],
"school": {
"id": 3,
"name": "Arts and Sciences",
"dean": null,
"createdAt": null,
"updatedAt": null
},
"facultyPhoto": null,
"primary": false
},
{
"userId": 5595,
"collectionRole": "faculty",
"regions": [
{
"id": 3,
"region": "Germany 1",
"regionalDirectorFirstName": "aaaa",
"regionalDirectorLastName": "bbbb",
"associateDirectorFirstName": null,
"associateDirectorLastName": null,
"division": {
"id": 2,
"name": "Europe",
"deletedFlag": false,
"createdAt": null,
"updatedAt": null
},
"deletedFlag": false,
"createdAt": null,
"updatedAt": null
}
],
"school": {
"id": 3,
"name": "Arts and Sciences",
"dean": null,
"createdAt": null,
"updatedAt": null
},
"facultyPhoto": null,
"primary": true
}
]
}
解决方案
您可以使用org.json.JSONObject
Response response = when()
.get("api_url")
.then()
.extract().response();
JSONObject jsonObject = new JSONObject(response.getBody().asString());
JSONArray jsonArray = jsonObject.getJSONArray("userRoleInfoList");
List<String> stringList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
stringList.add(jsonArray.getJSONObject(i).getString("collectionRole"));
}
System.out.println(stringList);
这将输出[program-chair, faculty]
推荐阅读
- python - 在熊猫中选择以相似字母开头的多列
- .net - SignalR 必须使用适当的属性或方法修改“用户代理”标头
- swift - 是否可以在带有 M1 的 Mac 上使用 CGEvent for iOS 应用程序模拟点击?
- html - 使用 ajax 从 django 的数据库中提取数据,然后在视图中显示
- html - 带引导容器的半宽背景图像
- flutter-dependencies - Mac中的Flutter Path设置问题
- azure-analysis-services - AnalysisServices:无法查询列的内部支持结构,因为它们未被处理。请刷新或重新计算表格
- node.js - 解耦账户和用户服务
- python - 一个数据帧对另一个不同维度的数据帧的影响
- java - mxn 矩阵的中心索引。如果中心不存在,则返回离中心最近的正方形的索引,整数最大