Home > OS >  How to save hashmap of hashmap
How to save hashmap of hashmap

Time:03-03

I am passing these data on socket and I have data in Hashmap of Hashmap. I tried to pass it but couldn't get.How to pass data on socket of Hashmap of Hashmap. like below

{"bot_id":"a0ddc016-bcb1-4c41-a2ba-9d2c3a6a1763","curr_id":"99501f27-54c7-4c0a-9b9b-598a5c71d374","data":{"target_id":"59f0048b-b497-4c6f-afb3-1457d54ba847"},"first_name":"System","last_name":"Message","room_id":"b3d026de-2c13-438b-a8c4-8f40c3d67b2a","user":"bot"}

I am unable to pass this one on socket. "data":{"target_id":"59f0048b-b497-4c6f-afb3-1457d54ba847"},

public void showToken(TokanGenerationModal jsonObject) {

    targetId=jsonObject.getTargetId().toString();
    room_id=jsonObject.getRoom_id().toString();
    owner_Id=jsonObject.getOwner_id().toString();
    curr_id=jsonObject.getId();

    Map<String,String> appLeadHashMap = new HashMap<>();
    appLeadHashMap.put("bot_Id", bot_Id);
    appLeadHashMap.put("curr_id", curr_id);
    appLeadHashMap.put("first_name","System");
    appLeadHashMap.put("last_name","Message");
    appLeadHashMap.put("room_id",room_id);
    appLeadHashMap.put("user","bot");
    appLeadHashMap.put("data",dataHashMap.put("targetId",targetId));
    
    start(owner_Id, room_id);

    session_id=jsonObject.getSession_token().toString();
    if((NetworkUtilities.isInternet(this)))
    {
        tokenPresenter.getMessage(targetId,room_id,session_id,this);
    }
    else 
    {
        Toast.makeText(this, "Check Internet connectivity.", Toast.LENGTH_SHORT).show();
    }

}

CodePudding user response:

So you have two HashMaps: appLeadHashMap and dataHashMap and you want to place the seconds into the first as a value?

Well your appLeadHashMap is a HashMap<String,String> meaning it will only accept String values! It will not accept a HashMap value.

To make this work modify your appLeadHashMap to accept any values:

Map<String,Object> appLeadHashMap = new HashMap<>();

There is also a nasty bug in your code on this line:

appLeadHashMap.put("data",dataHashMap.put("targetId",targetId));

Here, you add the result of put method into your hash map, and not the dataHashMap itself. Looks like you want to add a key-value pair to the inner hash map first, then you want to add it to the our hash map. To do so, use this:

Map<String,Object> appLeadHashMap = new HashMap<>();
appLeadHashMap.put("bot_Id", bot_Id);
appLeadHashMap.put("curr_id", curr_id);
appLeadHashMap.put("first_name","System");
appLeadHashMap.put("last_name","Message");
appLeadHashMap.put("room_id",room_id);
appLeadHashMap.put("user","bot");
//add key to dataHashMap first
dataHashMap.put("targetId",targetId)
//finally, add the outer hash map to the inner
appLeadHashMap.put("data", dataHashMap);

Please be mindful that now you will have to cast your objects that you retreive from the appLeadHashMap, because it has value type Object.

  • Related