get particular value by key from Hashmap in velocity template

daisy :

i have a java code like below

  public static void main(String args[]) throws Exception {
    VelocityEngine engine = new VelocityEngine();
    engine.init();

    Template template = engine.getTemplate("userinfo.vm");

    VelocityContext vc = new VelocityContext();

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

    jsonResponse.put("44", "United Kingdom");
    jsonResponse.put("33", "France");
    jsonResponse.put("49", null);

    vc.put("userList", jsonResponse);
    StringWriter writer = new StringWriter();
    template.merge(vc, writer);

    System.out.println(writer);
}

in .vm file

#foreach ($mapEntry in $userList.entrySet())
$!{mapEntry.get("44")}
#end

Here i am trying to get particular value using above code, but its not giving expected output

My expected output is

 United Kingdom
pleft :

Use this code to iterate through your map values. It is almost the same as yours however pay attention to: $userList.keySet() instead of $userList.entrySet() and $!{userList.get($mapKey )} instead of $!{mapEntry.get("44")}

#foreach($mapKey in $userList.keySet())
    $!{userList.get($mapKey )}
#end

If you want just to access a specific value of your map try this:

$!{userList.get("44")}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=80279&siteId=1