0

Actually I am trying to parse the two api's json data in the same class. I know it should not be done but this is the requirement. I am getting data from the hashmap. And I want to add it in the table.But the problem is program adds only last item in table. but while debugging it's giving all values.

Here is my code:

public class services2
{
    public Map<Object, Object> getReportees(String idOfEmp) throws  Exception {
        ......
        if(resp.getStatus() != 200){
            System.err.println("Unable to connect to the server");
        }
        String output = resp.getEntity(String.class);
        //Store the JSON objects in an array
        //Get the index of the JSON object and print the values as per the index
        JSONParser parse = new JSONParser();
        JSONObject jobj = (JSONObject)parse.parse(output);
        JSONArray jsonarr_1 = (JSONArray) jobj.get("list");
        Map<Object, Object> map=new HashMap<Object,Object>();
        for(int i=0;i<jsonarr_1.size();i++){
            JSONObject jsonobj_1 = (JSONObject)jsonarr_1.get(i);
            JSONObject jive      = (JSONObject)jsonobj_1.get("jive");
            String var   = jive.get("username").toString();
            values = var;
            if(resp1.getStatus() != 200){
                system.err.println("Unable to connect to the server");
            }
            String output1 = resp1.getEntity(String.class);
            JSONObject jobjs = (JSONObject) new JSONParser().parse(output1);

            JSONArray jsonarr_11 = (JSONArray) jobjs.get("issues");
            System.out.println("count"+jsonarr_11.size());
            Object obj3 = jsonarr_11.size();
            int counter = (Integer) obj3;
            System.out.println(counter);

            Object obj1 = jsonobj_1.get("displayName");
            Object obj2 = jive.get("username");

            map.put(obj1, obj2);
            map.put("obj3", obj3);
            System.out.println(obj3);
        }
        return  map;  //for the map obj3 return only the last count that is 1
    }
} 

Here when I am trying to send the map obj3. I am getting only the last value. I actually want the count in a correct way.

                    This is the output:
             Number  id     username       count
               1    A12345  Anagha R        1
               2    M12345  Madhusudan S    1
               3    AT12345 Amreen Taj      1
                   Expected output is:
               1    A12345  Anagha R        0
               2    M12345  Madhusudan S    0
               3    AT12345 Amreen Taj      1

When I am trying to print in the console it is giving me the right count but when I am trying through the map it is sending only the last value that is count 1.

Cœur
  • 37,241
  • 25
  • 195
  • 267
RAMYA
  • 89
  • 1
  • 8
  • Didn't you ask same question and have accept answer a day or two ago? https://stackoverflow.com/questions/54203266/how-to-return-more-than-two-objects-from-java-method-to-a-servlet – Ori Marko Jan 17 '19 at 05:57
  • Actually here the problem is not returning more than two map objects rather the last value is only being passed from the map object. – RAMYA Jan 17 '19 at 06:00

2 Answers2

0

The problem is in below line of code:

map.put("obj3", obj3);

Each time you are replacing the value for "Obj3" instead of incrementing the existing value of the map.

Instead what you need to do is:

if(map.containsKey("obj3")
{
    Object obj3 = map.get("obj3");
    //put your logic of incrementing or adding the count 
    //Lets say it is new Value
    map.put("obj3",newValue)
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kavya
  • 19
  • 4
  • Actually I am not fetching any key nor value from the second api rather I am just taking the arraysize which will give me the count.Then how can I implement it?? – RAMYA Jan 17 '19 at 06:32
0

You can try using a list of a map like the following:

List<HashMap<Object,Object>>
Cœur
  • 37,241
  • 25
  • 195
  • 267