如何将 Rhino-JavaScript 数组转换为 Java 数组
我有以下内容:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
jsEngine.eval("function getArray() {return [1,2,3,4,5];};");
Object result = jsEngine.eval("getArray();");
如何将 sun.org.mozilla.javascript.internal.NativeArray 类型的结果对象转换为相应的 java 数组?有人可以向我展示一个工作代码示例吗?它应该适用于字符串和整数数组。另外,如果知道在哪里可以找到 rhino 引擎和 java 之间的其他数据类型转换,那就太好了。
顺便说一句,我知道这个页面,但我真的在寻找一个有效的代码示例。
I have the following:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
jsEngine.eval("function getArray() {return [1,2,3,4,5];};");
Object result = jsEngine.eval("getArray();");
How can i convert the result object which is of type sun.org.mozilla.javascript.internal.NativeArray to a corresponding java array? Can somone show me a working code sample where this is done? It should work for String and Integer arrays. Plus, it would be great to know where to look for other data type conversions between the rhino engine and java.
Btw, i know this page but i'm really looking for a working code sample.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定第一次提出这个问题时是否是这种情况,但
NativeArray
实现了java.util.List
接口。因此,转换为真正的 Java 数组的简单方法是:I'm not sure if it was the case when this question was first asked, but
NativeArray
implements thejava.util.List
interface. A simple way to convert to a real Java array is therefore:如果 Javascript 在您的控制之下,您可以按照 此文档。因此,要调整您的示例,例如:
尽管如此,如果您无法更改 Javascript,则此方法将不起作用,并且您[i]将[/i]拥有一个 sun.org.mozilla.javascript.internal 的实例。 NativeArray 作为您的
result
变量。在这一点上,我认为您只需要使用它公开的任何公共方法直接转换和处理它;它可能不漂亮,但我没有看到任何其他选择。特别是,我认为在很好的 Rhino 级别上您唯一可以保证的是它将是 Scriptable 的实例(可能是 ScriptableObject ),这对您没有帮助将其用作数组。凯文的答案看起来不错到这里的方式(与我刚刚要编辑的内容类似!:-))
If the Javascript is under your control, you could do the transformation there, as per this document. So to adapt your example, something like:
Although, if you can't change the Javascript this approach won't work, and you [i]will[/i] have an instance of sun.org.mozilla.javascript.internal.NativeArray as your
result
variable. At which point I think you just need to cast and deal with it directly, using whatever public methods it exposes; it's probably not pretty but I don't see any other options. In particular I think the only thing you can guarantee at the nice Rhino level is that it will be an instance ofScriptable
(and probablyScriptableObject
), which doesn't help you use it as an Array.Kevin's answer looks like a good way to go here (and is similar to what I was just about to edit in! :-))
就我而言,我想在脚本中生成一个 Java 数组。 (此用例也符合问题。)
遵循 创建Java 数组,我想出了
In my case I wanted to produce a Java array within the script. (This use case also matches the question.)
Following Creating Java Arrays, I came up with
使用 JASON 作为数据中间体的通用解决方案:
General solution using JASON as data intermediate: