java - 如何将两个 map 添加到列表中

标签 java

代码是: 如何将 map 添加到列表中

   public List<Map<Object, Object>> getReportees(String idOfEmp) throws Exception {

    JSONArray jsonarr_s = (JSONArray) jobj.get("list");
    Map<Object, Object> map = new HashMap<Object, Object>();
    if (jsonarr_s.size() > 0) {

        // Get data for List array
        for (int i = 0; i < jsonarr_s.size(); i++) {

            JSONObject jsonobj_1 = (JSONObject) jsonarr_s.get(i);
            JSONObject jive = (JSONObject) jsonobj_1.get("jive");

            Object names = jsonobj_1.get("displayName");
            Object userid = jive.get("username");
            map.put(names, userid);                  //return the map with the key value pairs
            map = new HashMap<Object, Object>();
            String UserId = userid.toString();
            String output1 = resp1.getEntity(String.class);
            JSONObject jobjs = (JSONObject) new JSONParser().parse(output1);

            // Store the JSON object in JSON array as objects (For level 1 array element i.e
            // issues)
            JSONArray jsonarr_new = (JSONArray) jobjs.get("issues");
            int numofjiras = jsonarr_new.size();  //this jira count must be mapped to the name and id
            map.put("count", numofjiras);
        }

        return map;
    } else {
        map.put("errorcheck", msg);
    }
    return map;
   }
}
   I want the output like:

     Name     id    count
     AJ     235457   2
     Geet   637571   0

实际上我正在键值对中获取名称和ID。然后我尝试将每个ID传递给一个API,它将给我计数。那么我如何返回所有字段,即名称,ID和计数。所以在这里,我试图像这个用户 ID 和名称一样进行映射,这就是计数。我们如何才能实现它。请帮忙。先谢谢了。

最佳答案

我认为您可以尝试创建一个新类来表示输出中的每一行。例如,您可以创建一个 Employee 类,如下所示:

public class Employee {
    private long id;
    private String name;
    private int issueCount;

    //getters and setters
}

然后,您可以使用此类并将 JSONArray 数组中的值分配给它。一旦获得“count”的值,您就可以将 Employee 对象添加到 map (或列表)中。

希望这有帮助。

关于java - 如何将两个 map 添加到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54322121/

相关文章:

java - 从逗号分隔属性列表创建数组的更优雅的解决方案?

java - 相等的子字符串值不返回预期结果

java - Groovy 类的 XML 序列化

java - 使用外部属性运行 Jar 文件

java - 正则表达式在PIG中使用双引号

java - BuilderExample 类型未定义方法 builder()

Java 负向前瞻不起作用

java - 为什么这个静态嵌套类不能在一台计算机上的 Java 中运行?

java - Spring Boot插件需要Gradle 4.10或更高版本。当前版本是Gradle 4.1

java - Android Studio : android. jar 缺少一些类