簡體   English   中英

使用 Rest-Assured 和 java 從 JSON 響應中獲取特定數據

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

如何使用 Rest Assured 獲取父節點["Name": "PriorityUId"]下的子節點EntityPropertyValueProductPropertyValueUIdProductPropertyValueDisplayName

下面是代碼片段

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

我正在做一個系統輸出,我得到了整個下面的數據塊作為響應。

{
      "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 視圖

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM