如何将 Rhino-JavaScript 数组转换为 Java-Arrays
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1433382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Rhino-JavaScript arrays to Java-Arrays
提问by Chris
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 samplewhere 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.
如何将 sun.org.mozilla.javascript.internal.NativeArray 类型的结果对象转换为相应的 java 数组?有人可以向我展示完成此操作的工作代码示例吗?它应该适用于字符串和整数数组。另外,知道在哪里可以查找 rhino 引擎和 java 之间的其他数据类型转换会很棒。
Btw, i know this pagebut i'm really looking for a working code sample.
顺便说一句,我知道这个页面,但我真的在寻找一个有效的代码示例。
采纳答案by Kevin
NativeArray arr = (NativeArray) result;
Object [] array = new Object[(int) arr.getLength()];
for (Object o : arr.getIds()) {
int index = (Integer) o;
array[index] = arr.get(index, null);
}
回答by Andrzej Doyle
If the Javascript is under your control, you could do the transformation there, as per this document. So to adapt your example, something like:
如果 Javascript 在您的控制之下,您可以根据本文档在那里进行转换。因此,要调整您的示例,例如:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
jsEngine.eval("function getArray() {return [1,2,3,4,5];};");
String convertFuncSrc =
"function convertArray(type, arr) {"
+ " var jArr = java.lang.reflect.Array.newInstance(type, arr.length);"
+ " for (var i = 0; i < arr.length; i++) { "
+ " jArr[i] = arr[i];"
+ " }"
+ " return jArr;"
+ "};";
jsEngine.eval(convertFuncSrc);
Object result = jsEngine.eval("convertArray(java.lang.Integer.TYPE, getArray());");
int[] javaArray = (int[])result;
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 of Scriptable
(and probably ScriptableObject
), which doesn't help you use it as an Array.
但是,如果您无法更改 Javascript,则此方法将不起作用,并且您 [i] 将 [/i] 有一个 sun.org.mozilla.javascript.internal.NativeArray 实例作为您的result
变量。在这一点上,我认为您只需要使用它公开的任何公共方法直接转换和处理它;它可能不漂亮,但我看不到任何其他选择。特别是,我认为在不错的 Rhino 级别您唯一可以保证的是它将是Scriptable
(并且可能是ScriptableObject
)的实例,这并不能帮助您将其用作数组。
Kevin's answerlooks like a good way to go here (and is similar to what I was just about to edit in! :-))
凯文的答案看起来是一个很好的方法(并且类似于我即将编辑的内容!:-))
回答by Dave Hartnoll
I'm not sure if it was the case when this question was first asked, but NativeArray
implements the java.util.List
interface. A simple way to convert to a real Java array is therefore:
我不确定第一次问这个问题时是否是这种情况,但NativeArray
实现了java.util.List
接口。因此,转换为真正的 Java 数组的简单方法是:
Object[] array = ((List<?>) result).toArray();
回答by eurojet
General solution using JASON as data intermediate:
使用 JASON 作为数据中间体的通用解决方案:
Object result = jsEngine.eval("JSON.stringify(getArray())");
JsonReader jsonReader = Json.createReader(new StringReader(result.toString()));
JsonArray jsonArray = jsonReader.readArray();
jsonReader.close();
int[] numbers = new int[jsonArray.size()];
int index = 0;
for(JsonValue value : jsonArray){
numbers[index++] = Integer.parseInt(value.toString());
}