1

I have a Map<String, String> values which returns me key-pair values, I want each values as a string.

For example output should be like :

String a = "map value 1"
String b = "map value 2"

How do I make it, should I use iterator, if yes then how can I get each values in String?

Output :

11-30 14:05:03.344 21403-21403/com.example D/getActionValue: Map Values are : {com.ibm.mce.sdk.NOTIF_SOURCE_ID=-1638692152, com.ibm.mce.sdk.NOTIF_SOURCE_PAYLOAD={"attribution":"Campaign Name Group"}, value={"hasLoyaltyId":"please enter name"}, com.ibm.mce.sdk.NOTIF_SOURCE={"subject":"Title","message":"Notification Message","notification-action":{"name":"go to screen","value":{"hasLoyaltyId":"please enter name"},"type":"gotoscreen"}}}

After 1st level Iteration, i am still not getting values, coz it contains multiple values within which you can see in output

11-30 16:19:36.998 9429-9429/com.teleca.sam.engine D/getActionValue: Attribution is : Campaign Name 30
11-30 16:19:36.998 9429-9429/com.teleca.sam.engine D/getActionValue: Map Values are : {com.ibm.mce.sdk.NOTIF_SOURCE_ID=1671068670, com.ibm.mce.sdk.NOTIF_SOURCE_PAYLOAD={"attribution":"Campaign Name 30"}, value={"hasLoyaltyId":"please enter name "}, com.ibm.mce.sdk.NOTIF_SOURCE={"subject":"Title 1","message":"Notification Message 1","notification-action":{"name":"go to screen","value":{"hasLoyaltyId":"please enter name "},"type":"gotoscreen"}}}
11-30 16:19:36.999 9429-9429/com.example D/getActionValue: key : com.ibm.mce.sdk.NOTIF_SOURCE_ID
11-30 16:19:36.999 9429-9429/com.example D/getActionValue: value : + value
11-30 16:19:36.999 9429-9429/com.example D/getActionValue: key : com.ibm.mce.sdk.NOTIF_SOURCE_PAYLOAD
11-30 16:19:36.999 9429-9429/com.example D/getActionValue: value : + value
11-30 16:19:36.999 9429-9429/com.example D/getActionValue: key : value
11-30 16:19:36.999 9429-9429/com.example D/getActionValue: value : + value
11-30 16:19:36.999 9429-9429/com.example D/getActionValue: key : com.ibm.mce.sdk.NOTIF_SOURCE
11-30 16:19:36.999 9429-9429/com.example D/getActionValue: value : + value

2 Answers 2

3

Yes you can as follow,

for (Map.Entry<String, Object> e : hashmap.entrySet()) {
        String key = e.getKey();
        Object value = e.getValue();
        String a=e.getKey()+" "+e.getValue();
        Log.e("Map",a);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I got that, I made changes, I have another worry here. The value also contains multiple values within, which I again need to iterate?? Let me put output in question
yes, you need to iterate again as follows JSONObject menu = jObject.getJSONObject(e.getKey()); Map<String,String> map = new HashMap<String,String>(); Iterator iter = menu.keys(); while(iter.hasNext()){ String key = (String)iter.next(); String value = menu.getString(key); map.put(key,value); }
2

You could just iterate through the Map. The documentation for entrySet states:

Returns a Set view of the mappings contained in this map.

So based on that, you can use it in your iterator to do what you want.

Iterator it = values.entrySet().iterator();
while (it.hasNext()) 
{
    Map.Entry pair = (Map.Entry)it.next();
    System.out.println(pair.getKey() + " = " + pair.getValue());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.