【问题标题】:Getting unique attributes from a JSON API using Rest Assured使用 Rest Assured 从 JSON API 获取唯一属性
【发布时间】:2019-03-01 13:32:46
【问题描述】:

我有一个 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。

【问题讨论】:

  • 你总是引用路径locations_data,它甚至不在你的json中。这应该如何工作?
  • 我现在已经修好了。请查看更新后的问题。
  • 我不熟悉 JsonPath 但records 应该返回一个对象而不是字符串或字符串列表。假设它像其他 json 库一样工作,获取 records.0.emp_type 可能看起来像 response.jsonPath().getMap("records").get("0").get("emp_type")。如果可行,您可以从那里开始工作,即遍历 records 中的所有元素并根据需要处理它们的 emp_type
  • 它返回一个java.util.ArrayList cannot be cast to java.util.Map 错误
  • 也许你可以试试这个 List emp_types = Arrays.asList(response.jsonPath().getString("emp_type").split("\\s*,\\s*") );

标签: java rest-assured rest-assured-jsonpath


【解决方案1】:

你可以试试这个方法:

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

【讨论】:

  • @demouser123 是的,我忘了说我修复了你的 json。所提供的内容无效。如果你能更新它,我会尽量适应答案
  • 是的。我已经修复了 json
【解决方案2】:

如 cmets 部分所述,您可以先获取“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&lt;String&gt; unique_emp_types = emp_types.stream().distinct().collect(Collectors.toList());

【讨论】:

    【解决方案3】:

    试试这个代码:

                    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>

    【讨论】:

    • JSONArray jsonArray = new JSONArray(json); 抛出错误。
    【解决方案4】:

    尝试使用:

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

    【讨论】:

    • 这为 get 方法中使用的 * 提供了一个意外的令牌错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2020-12-23
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多