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

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


   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 和名称进行映射,这是计数。我们如何才能实现它。请帮助。谢谢。


慕工程0101907
浏览 159回答 2
2回答

千巷猫影

我认为您可以尝试创建一个新类来表示输出中的每一行。例如,您可以Employee像这样创建一个类:public class Employee {&nbsp; &nbsp; private long id;&nbsp; &nbsp; private String name;&nbsp; &nbsp; private int issueCount;&nbsp; &nbsp; //getters and setters}然后,您可以使用此类并将 JSONArray 数组中的值分配给它。一旦获得“count”的值,您就可以将 Employee 对象添加到地图(或列表)中。希望这可以帮助。

慕姐8265434

您需要先声明一个本地列表,然后返回该列表:&nbsp; &nbsp; public List<Map<Object, Object>> getReportees(String idOfEmp) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonarr_s = (JSONArray) jobj.get("list");&nbsp; &nbsp; &nbsp; &nbsp; Map<Object, Object> map = new HashMap<Object, Object>();&nbsp; &nbsp; &nbsp; &nbsp; List<Map<Object, Object>> resultList = new ArrayList<Map<Object,Object>>();&nbsp; &nbsp; &nbsp; &nbsp; if (jsonarr_s.size() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get data for List array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < jsonarr_s.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonobj_1 = (JSONObject) jsonarr_s.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jive = (JSONObject) jsonobj_1.get("jive");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object names = jsonobj_1.get("displayName");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object userid = jive.get("username");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(names, userid);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //return the map with the key value pairs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String UserId = userid.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String output1 = resp1.getEntity(String.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jobjs = (JSONObject) new JSONParser().parse(output1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Store the JSON object in JSON array as objects (For level 1 array element i.e&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // issues)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonarr_new = (JSONArray) jobjs.get("issues");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int numofjiras = jsonarr_new.size();&nbsp; //this jira count must be mapped to the name and id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put("count", numofjiras);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultList.add(map);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put("errorcheck", msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultList.add(map);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return resultList;&nbsp; &nbsp; }根据您的结果,尽管您应该考虑将数据对象翻转为&nbsp; &nbsp; Map<Object, List<Object>>其中地图中的第一个对象是它们的名称,然后列表将包含两个对象[id,count]。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java