user45678
user45678

Reputation: 1514

How to obtain particular value from Map in a "String" format

I have a Map :

Map<String, String> value

How to obtain particular value "String" ----> "type" from json value, how do i obtain it?

I tried below code, which only returns me KEY and not VALUE

Iterator it = payload.entrySet().iterator();
        while (it.hasNext())
        {
            Map.Entry pair = (Map.Entry)it.next();
            System.out.println(pair.getKey() + " = " + pair.getValue());
            String key = pair.getKey().toString();
            String value = pair.getValue().toString();
            Log.d("getActionValue", "key : " + key);
            Log.d("getActionValue", "value : + value");
        }

Upvotes: 0

Views: 115

Answers (4)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

I want that particular "String" ----> "type" from value, how do i obtain it?

payload Map contains JSONObject's as value of every key. so to get type need to first convert String to JSONObject then get value using type key:

JSONObject jsonObject=new JSONObject(value);
String strType="";
if(jsonObject.has("notification-action")){
   JSONObject jsonObjectnotification=jsonObject.optJSONObject
                                         (""notification-action"");
   if(jsonObjectnotification.has("type")){
      strType=jsonObjectnotification.optString("type");
   }
}

Upvotes: 1

nickmesi
nickmesi

Reputation: 180

You logging it wrong.

Should be:

Log.d("getActionValue", "value : " + value);

Upvotes: 1

karim mohsen
karim mohsen

Reputation: 2254

Your problem is here Log.d("getActionValue", "value : + value"); it should be Log.d("getActionValue", "value : "+ value);

Try this for loop :-

for (Map.Entry<String, String> entry : payload.entrySet())
{
    Log.d("getActionValue", "key : " + entry.getKey());
    Log.d("getActionValue", "value :" + entry.getValue());
}

Upvotes: 2

yennsarah
yennsarah

Reputation: 5517

You don't access your variable in your output, since value is still in in the string. Change like this:

Log.d("getActionValue", "value : " + value);

Upvotes: 4

Related Questions