RAMYA
RAMYA

Reputation: 89

How to add two maps to the List

Code is: How can to add the Map to the List

   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

Actually I am getting the Name and id in key value pairs.Then I am trying to pass each id to an api which will give me the count.So how can I return all the fileds i.e Name ,id and count.So here I am trying to map like for this Userid and Name this is the count.How can we acheive it.Plesae help.Thanks in advnce.

Upvotes: 0

Views: 139

Answers (2)

Chris Toh
Chris Toh

Reputation: 88

You would need to declare a local list first and then return that list:

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

Based on your results though you should consider flipping your data objects to instead be

    Map<Object, List<Object>>

where the first Object in the map which is they key the name and then the list would contain two objects [id, count].

Upvotes: 0

Ben
Ben

Reputation: 66

I think you can try creating a new class to represent each row in your output. For example, you can create an Employee class like this:

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

    //getters and setters
}

You can, then, use this class and assign the values from the JSONArray arrays to it. Once you get the value for "count", you can just add the Employee object to the map (or list).

Hope this helps.

Upvotes: 1

Related Questions