assured和java从json响应获取特定数据

jgwigjjp  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(338)

如何获取子节点 EntityPropertyValue , ProductPropertyValueUId , ProductPropertyValueDisplayName 在父节点下 ["Name": "PriorityUId"] 使用“放心”?
下面是代码片段

  1. RequestSpecification request = RestAssured.given();
  2. request.header("Content-Type", "application/json");
  3. JSONObject requestParams = new JSONObject();
  4. requestParams.put("data", "somedata");
  5. request.body(requestParams.toJSONString());
  6. Response response = request.post("url");
  7. List<Map<String, String>> epv = response.jsonPath().getList("EntityPropertyValues");
  8. for(int i=0;i<epv.size();i++)
  9. {
  10. if(epv.get(i).containsValue("PriorityUId"))
  11. {
  12. System.out.println(epv.get(i));
  13. break;
  14. }
  15. }

我正在做一个sysout,我得到了下面整个数据块的响应。

  1. {
  2. "Name": "PriorityUId",
  3. "Values": [
  4. {
  5. "EntityPropertyValue": "Critical",
  6. "ProductPropertyValueUId": "00000019-0000-0000-0000-000000000033",
  7. "ProductPropertyValueDisplayName": "Blocker"
  8. },
  9. {
  10. "EntityPropertyValue": "Critical",
  11. "ProductPropertyValueUId": "00000019-0000-0000-0000-000000000034",
  12. "ProductPropertyValueDisplayName": "Critical"
  13. },
  14. {
  15. "EntityPropertyValue": "High",
  16. "ProductPropertyValueUId": "00000019-0000-0000-0000-000000000035",
  17. "ProductPropertyValueDisplayName": "Major"
  18. }
  19. ],
  20. "DataTypeUId": "00100002-0007-0000-0000-000000000000",
  21. "DisplayName": "Priority"
  22. }

我怎样才能捕捉到我正在寻找的领域?
下面是整个json响应

  1. {
  2. "ProductInstance": "00000000-0000-0000-0000-000000000000",
  3. "DataTypeUId": null,
  4. "EntityPropertyValues": [
  5. {
  6. "Name": "ModifiedAtSourceByUser",
  7. "Values": [],
  8. "IsPropertyExists": true
  9. },
  10. {
  11. "Name": "ModifiedAtSourceOn",
  12. "Values": []
  13. },
  14. {
  15. "Name": "PriorityUId",
  16. "Values": [
  17. {
  18. "EntityPropertyValue": "Critical",
  19. "ProductPropertyValueUId": "00000019-0000-0000-0000-000000000033",
  20. "ProductPropertyValueDisplayName": "Blocker"
  21. },
  22. {
  23. "EntityPropertyValue": "Critical",
  24. "ProductPropertyValueUId": "00000019-0000-0000-0000-000000000034",
  25. "ProductPropertyValueDisplayName": "Critical"
  26. },
  27. {
  28. "EntityPropertyValue": "High",
  29. "ProductPropertyValueUId": "00000019-0000-0000-0000-000000000035",
  30. "ProductPropertyValueDisplayName": "Major"
  31. }
  32. ],
  33. "DataTypeUId": "00100002-0007-0000-0000-000000000000",
  34. "DisplayName": "Priority"
  35. }
  36. ]
  37. }
kpbwa7wx

kpbwa7wx1#

获取响应主体的jsonpath视图

  1. JsonPath js = response.jsonPath();

获取EntityPropertyValue的大小

  1. int size = js.getInt("EntityPropertyValues.size()");

循环遍历数组,直到找到所需的值,当前- PriorityUId 如果匹配,则使用jsonpath获取值,

  1. for (int i = 0; i < size; i++) {
  2. String detail = js.getString("EntityPropertyValues[" + i + "].Name");
  3. if (detail.equalsIgnoreCase("PriorityUId")) {
  4. List<Object> EntityPropertyValue = js
  5. .getList("EntityPropertyValues[" + i + "].Values.EntityPropertyValue");
  6. List<Object> ProductPropertyValueUId = js
  7. .getList("EntityPropertyValues[" + i + "].Values.ProductPropertyValueUId");
  8. List<Object> ProductPropertyValueDisplayName = js
  9. .getList("EntityPropertyValues[" + i + "].Values.ProductPropertyValueDisplayName");
  10. System.out.println("Values for EntityPropertyValue : " + EntityPropertyValue);
  11. System.out.println("Values for ProductPropertyValueUId : " + ProductPropertyValueUId);
  12. System.out.println("Values for ProductPropertyValueDisplayName : " + ProductPropertyValueDisplayName);
  13. break;
  14. }
  15. }
展开查看全部

相关问题