gpt4 book ai didi

java - 如何在java中使用StrSubstitutor进行嵌套Map

转载 作者:行者123 更新时间:2023-11-30 03:54:28 26 4
gpt4 key购买 nike

    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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com