java - 使用 Rest-Assured 和 java 从 JSON 响应获取特定数据

标签 java json rest-assured rest-assured-jsonpath

如何获取父节点["Name"下的子节点EntityPropertyValueProductPropertyValueUIdProductPropertyValueDisplayName :“PriorityUId”]使用放心?

下面是代码片段

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

我正在执行系统输出,并得到以下整个数据 block 作为响应。

{
      "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"
    }

如何捕获我正在寻找的字段?

下面是完整的 JSON 响应

{
  "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"
    }
  ]
}

最佳答案

获取响应正文的 JsonPath View

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

关于java - 使用 Rest-Assured 和 java 从 JSON 响应获取特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63730257/

相关文章:

javascript - 在 Angularjs 中发布 json 对象

javascript - 否 'Access-Control-Allow-Origin' - CORS

java - xmlPath 无法从 xml 中获取要列出的特定对象

json - 放心 : extracting list of values from Json Response using java

java - 通过 Ant 编译 GWT

java - Play 2.5.3 : Using dependency injection to get configuration values

java - 与树莓派 GPIO 接口(interface)

java - 我如何许可网络应用程序?

php - 在 Android 和 PHP Web 服务之间验证用户

spring-boot - Rest Assured 不使用自定义 ObjectMapper