To get the value of parameter you could do
String val = ((HashMap)map.get(0).get("parameters")).get("key");
although you will need to change
HashMap<String, String>
to HashMap<String, Object>
for this to work
Question
This line of code returns List<HashMap<String, String>>
List<HashMap<String,String>> map= restTemplate.postForObject(url,mvm,List.class);
And through this code, I can succesfully get the value of id and name in index[0]
.
List<HashMap<String, String>> map;
map.get(0).get("id");
map.get(0).get("name");
The Structure of the map
HashMap<"id","1">
<"name","john">
<"parameters",HashMap<"key", "val"> <"key2","val2">>
How can I get data from parameters? thanks.
Solution
To get the value of parameter you could do
String val = ((HashMap)map.get(0).get("parameters")).get("key");
although you will need to change
HashMap<String, String>
to HashMap<String, Object>
for this to work
OTHER TIPS
((HashMap)map.get(0).get("parameters")).get(map_key)
Try below code:
for(int i=0;i<map.size();i++){
map.get(i).get("id");
map.get(i).get("name");
}
Save each value to your needed variables.
You can only get string values from the Map since it's declared List<HashMap<String,String>>
. If it would say List<HashMap<String, Object>
you could do something like this:
package com.stackoverflow.maps;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class InnerMaps {
public InnerMaps(List<HashMap<String,Object>> outerList) {
System.out.println("InnerMaps.<init>");
for (HashMap<String, Object> outerMap: outerList) {
for (String key: outerMap.keySet()) {
Object value = outerMap.get(key);
if (value.getClass().getName().equals(HashMap.class.getName())) {
HashMap<String,String> hashMap = (HashMap<String,String>)value;
System.out.println(key + ", hashMap:" + hashMap );
} else {
System.out.println(key + " : " + value);
}
}
}
}
public static void main(String[] args) {
List<HashMap<String,Object>> theList = new ArrayList<>();
HashMap<String,String> innerHashmap = new HashMap<>();
innerHashmap.put("innerOne", "innerOneValue");
innerHashmap.put("innerTwo", "innerTwoValue");
HashMap<String, Object> outer = new HashMap<>();
outer.put("one", new String("One Value"));
outer.put("two", innerHashmap);
theList.add(outer);
InnerMaps app = new InnerMaps(theList);
}
}