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

huangapple go评论64阅读模式
英文:

Getting specific data from JSON response using Rest-Assured and java

问题

使用Rest Assured,我该如何获取位于父节点 ["Name": "PriorityUId"] 下的子节点 EntityPropertyValueProductPropertyValueUIdProductPropertyValueDisplayName

以下是代码片段:

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" 的节点,并获取其下的子节点数据。然后,你可以使用 entityPropertyValueproductPropertyValueUIdproductPropertyValueDisplayName 进行进一步处理。

英文:

How can I get the sub-nodes EntityPropertyValue,ProductPropertyValueUId,ProductPropertyValueDisplayName that come under the parent node [&quot;Name&quot;: &quot;PriorityUId&quot;] using Rest Assured ?

Below is the code snippet

RequestSpecification request = RestAssured.given();
request.header(&quot;Content-Type&quot;, &quot;application/json&quot;);
JSONObject requestParams = new JSONObject();
requestParams.put(&quot;data&quot;, &quot;somedata&quot;);  
request.body(requestParams.toJSONString());	
Response response = request.post(&quot;url&quot;);
 List&lt;Map&lt;String, String&gt;&gt; epv = response.jsonPath().getList(&quot;EntityPropertyValues&quot;);
		for(int i=0;i&lt;epv.size();i++)
		{
			if(epv.get(i).containsValue(&quot;PriorityUId&quot;))
			{
				System.out.println(epv.get(i));
				break;
				
			}
		}

I am doing a sysout and I get the entire below block of data in response.

{
      &quot;Name&quot;: &quot;PriorityUId&quot;,
      &quot;Values&quot;: [
					{
					  &quot;EntityPropertyValue&quot;: &quot;Critical&quot;,
					  &quot;ProductPropertyValueUId&quot;: &quot;00000019-0000-0000-0000-000000000033&quot;,
					  &quot;ProductPropertyValueDisplayName&quot;: &quot;Blocker&quot;
					},
					{
					  &quot;EntityPropertyValue&quot;: &quot;Critical&quot;,
					  &quot;ProductPropertyValueUId&quot;: &quot;00000019-0000-0000-0000-000000000034&quot;,
					  &quot;ProductPropertyValueDisplayName&quot;: &quot;Critical&quot;
					},
					{
					  &quot;EntityPropertyValue&quot;: &quot;High&quot;,
					  &quot;ProductPropertyValueUId&quot;: &quot;00000019-0000-0000-0000-000000000035&quot;,
					  &quot;ProductPropertyValueDisplayName&quot;: &quot;Major&quot;
					}
      ],
      &quot;DataTypeUId&quot;: &quot;00100002-0007-0000-0000-000000000000&quot;,
      &quot;DisplayName&quot;: &quot;Priority&quot;
    }

How can I capture the fields I am looking for?

Below is the entire JSON response

{
  &quot;ProductInstance&quot;: &quot;00000000-0000-0000-0000-000000000000&quot;,
  &quot;DataTypeUId&quot;: null,
  &quot;EntityPropertyValues&quot;: [
    {
      &quot;Name&quot;: &quot;ModifiedAtSourceByUser&quot;,
      &quot;Values&quot;: [],
      &quot;IsPropertyExists&quot;: true
    },
    {
      &quot;Name&quot;: &quot;ModifiedAtSourceOn&quot;,
      &quot;Values&quot;: []
    },
    {
      &quot;Name&quot;: &quot;PriorityUId&quot;,
      &quot;Values&quot;: [
					{
					  &quot;EntityPropertyValue&quot;: &quot;Critical&quot;,
					  &quot;ProductPropertyValueUId&quot;: &quot;00000019-0000-0000-0000-000000000033&quot;,
					  &quot;ProductPropertyValueDisplayName&quot;: &quot;Blocker&quot;
					},
					{
					  &quot;EntityPropertyValue&quot;: &quot;Critical&quot;,
					  &quot;ProductPropertyValueUId&quot;: &quot;00000019-0000-0000-0000-000000000034&quot;,
					  &quot;ProductPropertyValueDisplayName&quot;: &quot;Critical&quot;
					},
					{
					  &quot;EntityPropertyValue&quot;: &quot;High&quot;,
					  &quot;ProductPropertyValueUId&quot;: &quot;00000019-0000-0000-0000-000000000035&quot;,
					  &quot;ProductPropertyValueDisplayName&quot;: &quot;Major&quot;
					}
      ],
      &quot;DataTypeUId&quot;: &quot;00100002-0007-0000-0000-000000000000&quot;,
      &quot;DisplayName&quot;: &quot;Priority&quot;
    }
  ]
}

答案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(&quot;EntityPropertyValues.size()&quot;);

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 &lt; size; i++) {
		String detail = js.getString(&quot;EntityPropertyValues[&quot; + i + &quot;].Name&quot;);
		if (detail.equalsIgnoreCase(&quot;PriorityUId&quot;)) {
			List&lt;Object&gt; EntityPropertyValue = js
					.getList(&quot;EntityPropertyValues[&quot; + i + &quot;].Values.EntityPropertyValue&quot;);
			List&lt;Object&gt; ProductPropertyValueUId = js
					.getList(&quot;EntityPropertyValues[&quot; + i + &quot;].Values.ProductPropertyValueUId&quot;);
			List&lt;Object&gt; ProductPropertyValueDisplayName = js
					.getList(&quot;EntityPropertyValues[&quot; + i + &quot;].Values.ProductPropertyValueDisplayName&quot;);
			System.out.println(&quot;Values for EntityPropertyValue : &quot; + EntityPropertyValue);
			System.out.println(&quot;Values for ProductPropertyValueUId : &quot; + ProductPropertyValueUId);
			System.out.println(&quot;Values for ProductPropertyValueDisplayName : &quot; + ProductPropertyValueDisplayName);
			break;
		}
	}

huangapple
  • 本文由 发表于 2020年9月4日 03:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63730257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定