This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map data=[ | |
one:"john", | |
two:"mary" | |
] | |
String entry="two" | |
println data."${entry}" //"mary" |
However this trick doesnt work when nested '.' are inside de expression.
In this case you can do the following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map data=[ | |
one:[ | |
two:"2222", | |
three:"333" | |
] | |
] | |
String nestedExpression="one.two" | |
String output=nestedExpression.tokenize('.').inject(data) {v, k -> v."$k"} | |
println output // "2222" |
That's all!