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

问题描述 投票:0回答:4

我有一个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属性。我尝试了以下方法,但没有一个工作 -

方法-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。

java rest-assured rest-assured-jsonpath
4个回答
2
投票

你可以这样试试:

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

0
投票

试试这段代码:

                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>

0
投票

尝试使用:

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

0
投票

如评论部分所述,您可以先获取“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 Stream API

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

© www.soinside.com 2019 - 2024. All rights reserved.