为了账号安全,请及时绑定邮箱和手机立即绑定

如何将两个地图添加到列表中

如何将两个地图添加到列表中

慕工程0101907 2022-05-20 18:24:39
代码是:如何将地图添加到列表中   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 和名称进行映射,这是计数。我们如何才能实现它。请帮助。谢谢。
查看完整描述

2 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

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


public class Employee {

    private long id;

    private String name;

    private int issueCount;


    //getters and setters

}

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


希望这可以帮助。


查看完整回答
反对 回复 2022-05-20
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

您需要先声明一个本地列表,然后返回该列表:


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

        List<Map<Object, Object>> resultList = new ArrayList<Map<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


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


            }

            resultList.add(map);

        } else {

            map.put("errorcheck", msg);

            resultList.add(map);

        }

        return resultList;

    }

根据您的结果,尽管您应该考虑将数据对象翻转为


    Map<Object, List<Object>>

其中地图中的第一个对象是它们的名称,然后列表将包含两个对象[id,count]。


查看完整回答
反对 回复 2022-05-20
  • 2 回答
  • 0 关注
  • 158 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号