Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Rest-Assured和java从JSON响应获取特定数据_Java_Json_Rest Assured_Rest Assured Jsonpath - Fatal编程技术网

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

使用Rest-Assured和java从JSON响应获取特定数据,java,json,rest-assured,rest-assured-jsonpath,Java,Json,Rest Assured,Rest Assured Jsonpath,如何使用Rest Assured获取父节点下的子节点EntityPropertyValue、ProductPropertyValueUId、ProductPropertyValueDisplayName 下面是代码片段 RequestSpecification request = RestAssured.given(); request.header("Content-Type", "application/json"); JSONObject reque

如何使用Rest Assured获取父节点下的子节点
EntityPropertyValue
ProductPropertyValueUId
ProductPropertyValueDisplayName

下面是代码片段

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;
                
            }
        }
我怎样才能捕获我正在寻找的字段

下面是整个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视图

JsonPath js = response.jsonPath();
获取EntityPropertyValue的大小

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;
        }
    }
for(int i=0;i
Sonal-运气好吗?是的,我刚试过你提供的代码片段。工作得很有魅力。正是我想要的。非常感谢@WilfredClement。救了我一天:)
    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;
        }
    }