如何从 groovy 执行 Javascript 代码并以地图形式获取结果?
如何从 groovy 获取执行 javascript 代码的结果?我尝试了以下操作,但我总是返回字符串“world”。我本来期望的是一个物体或地图。
import javax.script.ScriptEngineManager
import javax.script.SimpleBindings
def manager = new ScriptEngineManager()
manager.getEngineByName("JavaScript").eval("""
{hello: name}
""", [name:'world'] as SimpleBindings)
How can I get the results of executed javascript code from groovy? I tried the following, but I always get back the string "world". I would have expected an object or map.
import javax.script.ScriptEngineManager
import javax.script.SimpleBindings
def manager = new ScriptEngineManager()
manager.getEngineByName("JavaScript").eval("""
{hello: name}
""", [name:'world'] as SimpleBindings)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单的!
您可以将对象映射到变量,然后返回该...
或者您可以跟踪
response
Map 对象,然后更新该...Groovy 版本: <代码>2.4.5
Java版本:
1.8.0_60
Easy!
You could map the object to a variable, and return that...
Or you could keep track of a
response
Map object, and update that...Groovy version:
2.4.5
Java version:
1.8.0_60
这有点棘手(我能找到的唯一解决方案是使用内部
sun.com
类):-/打印出:
感觉不是一种非常干净的做事方式(并且会例如,如果您有数组映射,可能需要一些工作)
但我能找到的最好的:-/
It's a bit tricky (and the only solution I can find is to use an internal
sun.com
class) :-/That prints out:
Doesn't feel a very clean way of doing things (and would probably require some work if you have a map of arrays for example)
But the best I can find :-/