Map<String, Object> baseMap = new HashMap<String, Object>();
baseMap.put("Name", "Raja");
Map<String, Object> address = new HashMap<String, Object>();
address.put("Street", "RAMA");
baseMap.put("Address", address);
StrSubstitutor strsub = new StrSubstitutor(baseMap);
String str = "This is ${Name} from ${Address.Street}";
System.out.println(strsub.replace(str));
输出为:
我是来自 ${Address.Street} 的 Raja
我需要的是:
这是 RAMA 的 Raja
我怎样才能得到这个?
最佳答案
您可能需要另一个 StrSubstitutor
实例来替换地址
:
StrSubstitutor strsub = new StrSubstitutor(baseMap);
StrSubstitutor baseSub = new StrSubstitutor(address);
String str = "This is ${Name} from ${Address.Street}";
System.out.println(baseSub.replace(strsub.replace(str)));
但我相信更好的解决方案是使用 sinlge baseMap
:
Map<String, Object> baseMap = new HashMap<String, Object>();
baseMap.put("Name", "Raja");
baseMap.put("Street", "RAMA");
StrSubstitutor strsub = new StrSubstitutor(baseMap);
String str = "This is ${Name} from ${Address.Street}";
System.out.println(strsub.replace(str));
关于java - 如何在java中使用StrSubstitutor进行嵌套Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23608507/