且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Groovy中使用字符串插值从带点表示法的嵌套地图的属性中获取值

更新时间:2023-11-14 09:55:34

您可以在.上分割路径(用正则表达式引用),然后减少在路径上使用您的数据.例如

You can split the path on . (quote it for the regexp) and then reduce over the path using your data. E.g.

def data = [a: [b: [c: 42]]]
def path = "a.b.c"

println path.split(/\./).inject(data){ m, p -> m?.getAt(p) }
// → 42