如何从 groovy 执行 Javascript 代码并以地图形式获取结果?

发布于 2024-11-15 13:34:49 字数 327 浏览 5 评论 0原文

如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落画卷 2024-11-22 13:34:49

简单的!

您可以将对象映射到变量,然后返回该...

import javax.script.*

def bindings = [name:'world']

def response = new ScriptEngineManager()
    .getEngineByName('javascript')
    .eval("var r = {hello:name}; r;", bindings as SimpleBindings)

println response.hello // -> world

或者您可以跟踪 response Map 对象,然后更新该...

import javax.script.*

def bindings = [name:'world',response:[:]]

new ScriptEngineManager()
    .getEngineByName('javascript')
    .eval("var r = {hello:name}; response.data = r;", bindings as SimpleBindings)

println bindings.response.data.hello // -> world

Groovy 版本: <代码>2.4.5

Java版本: 1.8.0_60

Easy!

You could map the object to a variable, and return that...

import javax.script.*

def bindings = [name:'world']

def response = new ScriptEngineManager()
    .getEngineByName('javascript')
    .eval("var r = {hello:name}; r;", bindings as SimpleBindings)

println response.hello // -> world

Or you could keep track of a response Map object, and update that...

import javax.script.*

def bindings = [name:'world',response:[:]]

new ScriptEngineManager()
    .getEngineByName('javascript')
    .eval("var r = {hello:name}; response.data = r;", bindings as SimpleBindings)

println bindings.response.data.hello // -> world

Groovy version: 2.4.5

Java version: 1.8.0_60

小伙你站住 2024-11-22 13:34:49

这有点棘手(我能找到的唯一解决方案是使用内部 sun.com 类):-/

import javax.script.ScriptEngineManager
import javax.script.SimpleBindings
import sun.org.mozilla.javascript.internal.NativeObject

// A Category to parse NativeObject into a Map
class NativeObjectParser {
  static Map asMap( NativeObject jsobj ) {
    jsobj.allIds.inject( [:] ) { map, key ->
      def value = jsobj.get( key, jsobj )
      // Handle nested maps
      map << [ (key):value instanceof NativeObject ? value.asMap() : value ]
    }
  }
}

// Your code as you had it before (apart from the JS defines a var, and returns that var object)
def manager = new ScriptEngineManager()
def ret = manager.getEngineByName("JavaScript").eval("""
    var r = { 'hello': name }
    r
""", [ name:'world' ] as SimpleBindings )

// Do the unwrapping
def map = use( NativeObjectParser ) {
  ret.asMap()
}

println map

打印出:

[hello:world]

感觉不是一种非常干净的做事方式(并且会例如,如果您有数组映射,可能需要一些工作)

但我能找到的最好的:-/

It's a bit tricky (and the only solution I can find is to use an internal sun.com class) :-/

import javax.script.ScriptEngineManager
import javax.script.SimpleBindings
import sun.org.mozilla.javascript.internal.NativeObject

// A Category to parse NativeObject into a Map
class NativeObjectParser {
  static Map asMap( NativeObject jsobj ) {
    jsobj.allIds.inject( [:] ) { map, key ->
      def value = jsobj.get( key, jsobj )
      // Handle nested maps
      map << [ (key):value instanceof NativeObject ? value.asMap() : value ]
    }
  }
}

// Your code as you had it before (apart from the JS defines a var, and returns that var object)
def manager = new ScriptEngineManager()
def ret = manager.getEngineByName("JavaScript").eval("""
    var r = { 'hello': name }
    r
""", [ name:'world' ] as SimpleBindings )

// Do the unwrapping
def map = use( NativeObjectParser ) {
  ret.asMap()
}

println map

That prints out:

[hello:world]

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 :-/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文