在groovy中使用eval
如何在 groovy 中使用 eval 来评估以下字符串:
{key1=keyval, key2=[listitem1, listitem2], key3=keyval2}
所有列表项和 keyval 都是一个字符串。
执行 Eval.me("{key1=keyval, key2=[listitem1, listitem2], key3=keyval2}") 给出以下错误:
不明确的表达式可能是无参数闭包表达式或隔离的开放代码块; 解决方案:添加显式的闭包参数列表,例如{it -> ...},或者通过给它一个标签来强制将其视为开放块,例如 L:{...} at
I Want to get HashMap
How can I use eval in groovy to evaluate the following String:
{key1=keyval, key2=[listitem1, listitem2], key3=keyval2}
All the list items and keyval is a String.
doing Eval.me("{key1=keyval, key2=[listitem1, listitem2], key3=keyval2}") is giving me the following error:
Ambiguous expression could be either a parameterless closure expression or an isolated open code block;
solution: Add an explicit closure parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L:{...} at
I want to get HashMap
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有没有办法获取JSON格式的数据?然后,您可以使用此处提到的解析器之一。
Is there no way you can get the data in JSON format? Then you could just use one of the parsers mentioned here.
您可以通过翻译一些字符来解析该字符串,并编写自己的绑定以在 Groovy 尝试查找变量名称时返回变量名称,如下所示
: 脆弱的解决方案,并以更友好的格式获取数据(正如@Stefan建议的那样)绝对是最好的方法......
You can parse that string by translating some of the characters, and writing your own binding to return variable names when Groovy tries to look them up, like so:
But this is a very brittle solution, and getting the data in a friendlier format (as suggested by @Stefan) is definitely the best way to go...