作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
代码是:如何将 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/
我是一名优秀的程序员,十分优秀!