英文:
Getting specific data from JSON response using Rest-Assured and java
问题
使用Rest Assured,我该如何获取位于父节点 ["Name": "PriorityUId"]
下的子节点 EntityPropertyValue
、ProductPropertyValueUId
和 ProductPropertyValueDisplayName
?
以下是代码片段:
List<Map<String, String>> epv = response.jsonPath().getList("EntityPropertyValues");
for (int i = 0; i < epv.size(); i++) {
if (epv.get(i).containsValue("PriorityUId")) {
List<Map<String, String>> values = (List<Map<String, String>>) epv.get(i).get("Values");
for (Map<String, String> value : values) {
String entityPropertyValue = value.get("EntityPropertyValue");
String productPropertyValueUId = value.get("ProductPropertyValueUId");
String productPropertyValueDisplayName = value.get("ProductPropertyValueDisplayName");
// 使用获取到的数据进行处理
}
break;
}
}
这段代码会遍历 JSON 中的 EntityPropertyValues
,找到具有特定值 "PriorityUId"
的节点,并获取其下的子节点数据。然后,你可以使用 entityPropertyValue
、productPropertyValueUId
和 productPropertyValueDisplayName
进行进一步处理。
英文:
How can I get the sub-nodes EntityPropertyValue
,ProductPropertyValueUId
,ProductPropertyValueDisplayName
that come under the parent node ["Name": "PriorityUId"]
using Rest Assured ?
Below is the code snippet
RequestSpecification request = RestAssured.given();
request.header("Content-Type", "application/json");
JSONObject requestParams = new JSONObject();
requestParams.put("data", "somedata");
request.body(requestParams.toJSONString());
Response response = request.post("url");
List<Map<String, String>> epv = response.jsonPath().getList("EntityPropertyValues");
for(int i=0;i<epv.size();i++)
{
if(epv.get(i).containsValue("PriorityUId"))
{
System.out.println(epv.get(i));
break;
}
}
I am doing a sysout and I get the entire below block of data in response.
{
"Name": "PriorityUId",
"Values": [
{
"EntityPropertyValue": "Critical",
"ProductPropertyValueUId": "00000019-0000-0000-0000-000000000033",
"ProductPropertyValueDisplayName": "Blocker"
},
{
"EntityPropertyValue": "Critical",
"ProductPropertyValueUId": "00000019-0000-0000-0000-000000000034",
"ProductPropertyValueDisplayName": "Critical"
},
{
"EntityPropertyValue": "High",
"ProductPropertyValueUId": "00000019-0000-0000-0000-000000000035",
"ProductPropertyValueDisplayName": "Major"
}
],
"DataTypeUId": "00100002-0007-0000-0000-000000000000",
"DisplayName": "Priority"
}
How can I capture the fields I am looking for?
Below is the entire JSON response
{
"ProductInstance": "00000000-0000-0000-0000-000000000000",
"DataTypeUId": null,
"EntityPropertyValues": [
{
"Name": "ModifiedAtSourceByUser",
"Values": [],
"IsPropertyExists": true
},
{
"Name": "ModifiedAtSourceOn",
"Values": []
},
{
"Name": "PriorityUId",
"Values": [
{
"EntityPropertyValue": "Critical",
"ProductPropertyValueUId": "00000019-0000-0000-0000-000000000033",
"ProductPropertyValueDisplayName": "Blocker"
},
{
"EntityPropertyValue": "Critical",
"ProductPropertyValueUId": "00000019-0000-0000-0000-000000000034",
"ProductPropertyValueDisplayName": "Critical"
},
{
"EntityPropertyValue": "High",
"ProductPropertyValueUId": "00000019-0000-0000-0000-000000000035",
"ProductPropertyValueDisplayName": "Major"
}
],
"DataTypeUId": "00100002-0007-0000-0000-000000000000",
"DisplayName": "Priority"
}
]
}
答案1
得分: 2
获取响应体的JsonPath视图
JsonPath js = response.jsonPath();
获取EntityPropertyValues的大小
int size = js.getInt("EntityPropertyValues.size()");
循环遍历数组,直到找到所需的值,目前是 - PriorityUId
如果匹配,则使用JsonPath获取值,
for (int i = 0; i < size; i++) {
String detail = js.getString("EntityPropertyValues[" + i + "].Name");
if (detail.equalsIgnoreCase("PriorityUId")) {
List<Object> EntityPropertyValue = js
.getList("EntityPropertyValues[" + i + "].Values.EntityPropertyValue");
List<Object> ProductPropertyValueUId = js
.getList("EntityPropertyValues[" + i + "].Values.ProductPropertyValueUId");
List<Object> ProductPropertyValueDisplayName = js
.getList("EntityPropertyValues[" + i + "].Values.ProductPropertyValueDisplayName");
System.out.println("Values for EntityPropertyValue: " + EntityPropertyValue);
System.out.println("Values for ProductPropertyValueUId: " + ProductPropertyValueUId);
System.out.println("Values for ProductPropertyValueDisplayName: " + ProductPropertyValueDisplayName);
break;
}
}
英文:
Get a JsonPath view of the response body
JsonPath js = response.jsonPath();
Get the size of EntityPropertyValues
int size = js.getInt("EntityPropertyValues.size()");
Loop through the array until you find the desired value, currently - PriorityUId
If it matches use JsonPath to fetch the values,
for (int i = 0; i < size; i++) {
String detail = js.getString("EntityPropertyValues[" + i + "].Name");
if (detail.equalsIgnoreCase("PriorityUId")) {
List<Object> EntityPropertyValue = js
.getList("EntityPropertyValues[" + i + "].Values.EntityPropertyValue");
List<Object> ProductPropertyValueUId = js
.getList("EntityPropertyValues[" + i + "].Values.ProductPropertyValueUId");
List<Object> ProductPropertyValueDisplayName = js
.getList("EntityPropertyValues[" + i + "].Values.ProductPropertyValueDisplayName");
System.out.println("Values for EntityPropertyValue : " + EntityPropertyValue);
System.out.println("Values for ProductPropertyValueUId : " + ProductPropertyValueUId);
System.out.println("Values for ProductPropertyValueDisplayName : " + ProductPropertyValueDisplayName);
break;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论