使用 Rest Assured 从 JSON API 获取唯一属性

Getting unique attributes from a JSON API using Rest Assured

我有一个 API ,它给出了这样的 JSON 响应

[{
"records": {
    "0": {
        "id": 123,
        "emp_type": "P",
        "emp_level": "8",
        "emp_id": "12345"
    },
    "1": {
        "id": 132,
        "emp_type": "Q",
        "emp_level": "1",
        "emp_id": "1234589"
    },
    "2": {
        "id": 132,
        "emp_type": "Q",
        "emp_level": "1",
        "emp_id": "1234589"
    },


    "3": {
        "id": 134,
        "emp_type": "Q",
        "emp_level": "3",
        "emp_id": "1231"
    }
}

}]

我想从此响应中找到所有唯一的 emp_type 属性。我尝试了以下方法,但其中 none 有效 -

接近-1

List<Map<String, String>> companies = response.jsonPath().getList("records");
        System.out.println(companies.get(0));

接近-2

List<Map<String, String>> companies = response.jsonPath().getList("records");
    System.out.println(companies.get(0).get("emp_type"));

接近-3

 Map<String, String> company = response.jsonPath().getMap("records");
        System.out.println(company.get("emp_type"));

方法-4

String username = response.jsonPath().getString("records[0]");
    System.out.println(username.indexOf(1));

方法 - 5

    String value = response.path("records").toString();
    System.out.println(value);

编辑 -1:修复了 JSON。

试试这个代码:

                try {   
                    URL url = new URL("http://mysafeinfo.com/api/data?list=englishmonarchs&format=json");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    BufferedReader read = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String line = read.readLine();
                    String json = "";
                    while(line!=null) {
                        json += line;
                        line = read.readLine();
                    }   

                    JSONArray jsonArray = new JSONArray(json);
                    for(int i=0; i<jsonArray.length(); i++) {

                       JSONObject a = jsonArray.getJSONObject(i);

                    }

                String a = jsonArray.toString();
                request.setAttribute("json",a);
                request.getRequestDispatcher("NewFile.jsp").forward(request, response);

            } catch (Exception e) {
                e.printStackTrace();
                out.write(e.toString());
            }

并在 jsp 文件中:

<script type="text/javascript"//-->
 var a='${json}';
 var test = '<%= (String) request.getAttribute("json") %>';
 
 var json =JSON.parse(test);

 var i;
 for (i = 0; i < json.length; i++) { 
  document.write( "<tr><td>"+json[i].id+"</td>");
  document.write("<td>"+ json[i].nm+"</td>");
  document.write("<td>" +json[i].cty+"</td>");
  document.write( "<td>"+json[i].hse+"</td>");
  document.write( "<td>"+json[i].yrs + "</td></td>");
  }
 
 </script>

你可以这样试试:

JsonPath jsonPath = new JsonPath(json);
List<Map<String, Map<String, Object>>> list = jsonPath.getList("records");
Set<String> uniqueValues = new HashSet<>();
for (Map<String, Map<String, Object>> map : list) {
  for (Map<String, Object> m : map.values()) {
    uniqueValues.add(String.valueOf(m.get("emp_type")));
  }
}
for (String unique : uniqueValues) {
  System.out.println(unique);
}

尝试使用:

List<String> empTypes = response.jsonPath().get("records.*.emp_type");

如评论区所述,可以先获取"emp_type"值,然后保持唯一。为此,您可以执行以下操作

从 JSON

中检索 emp_types 的列表
List<String> emp_types = 
Arrays.asList(response.jsonPath().getString("emp_type").split("\s*,\s*")); 

保留响应中的唯一值

1) 通过将列表转换为HashSet(一个集合只允许唯一值)

Set<String> unique_emp_types = new HashSet<String>(emp_types );

如果想将您的唯一值放入列表中并且您使用 Java 8 + 您可以使用 Java 流 API

2) List<String> unique_emp_types = emp_types.stream().distinct().collect(Collectors.toList());