3

I'm trying to convert a Javascript array in Java to a Java array. I'm using the javax.script package. I tested this example here, but the type "NativeArray" was not recognized: https://stackoverflow.com/a/1433489/975097

How can I get the NativeArray type to be recognized?

Community
  • 1
  • 1
Anderson Green
  • 25,996
  • 59
  • 164
  • 297
  • That "NativeArray" class in the other question is a Rhino thing, so if you're using the ScriptEngine mechanism I don't think it's available (at least not in any "clean" way). – Pointy Jan 13 '12 at 16:43
  • 1
    ... err, it might be `sun.org.mozilla.javascript.internal.NativeArray` – Pointy Jan 13 '12 at 16:44
  • 1
    Access restriction: The type NativeArray is not accessible due to restriction on required library /usr/lib/jvm/java-6-openjdk/jre/lib/rhino.jar – Anderson Green Jan 13 '12 at 19:21

4 Answers4

3

Rhino offers this:

https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Embedding_tutorial#usingJSObjs

Also Scriptable interface offers get() and set() so you can easily enumerate the properties of an object and add it to an array:

Scriptable arr = (Scriptable) result;
Object [] array = new Object[arr.getIds().length];
for (Object o : arr.getIds()) {
   int index = (Integer) o;
   array[index] = arr.get(index, null);
}

Same thing but not using NativeArray since that appears to be a Rhino specific thing. You could easily drop a breakpoint and see what type of object you were given then downcast to that. It's some sort of JS Array implementation that's pretty close to NativeArray.

Kenster
  • 18,710
  • 21
  • 68
  • 90
chubbsondubs
  • 34,812
  • 24
  • 97
  • 134
2

Per this answer it looks like your best bet is to write a JavaScript converter function which transforms the native JavaScript array into a Java array using Rhino's Java binding functionality. Note that you'll have to take some care to use the correct type when converting the individual elements.

[Edit] Here's a working example using a string array:

ScriptEngine js = new ScriptEngineManager().getEngineByName("JavaScript");
String ss[] = (String[]) js.eval(
    "(function() {" +
    "  var a = java.lang.reflect.Array.newInstance(java.lang.String, 3);" +
    "  a[0] = 'foo';" +
    "  a[1] = 'bar';" +
    "  a[2] = 'gah';" +
    "  return a;" +
    "})()");
System.out.println(Arrays.toString(ss)); // => [foo, bar, gah]
Community
  • 1
  • 1
maerics
  • 133,300
  • 39
  • 246
  • 273
  • Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.EvaluatorException: Can't find method java.lang.reflect.Array.newInstance(string,number). (#120) in at line number 120 at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:255) at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(RhinoScriptEngine.java:213) at JavascriptInJava.main(JavascriptInJava.java:75) – Anderson Green Jan 14 '12 at 05:28
  • I got the error above when I added this line: var jArr = java.lang.reflect.Array.newInstance(String, sortedParms.length); where sortedParms.length is the length of an array. It looks like the method is undefined, even though it was supposed to work in the example given in the link. Are there any workarounds for this? – Anderson Green Jan 14 '12 at 05:29
  • 1
    @AndersonGreen: try using the argument `java.lang.String`, per my updated answer. – maerics Jan 14 '12 at 15:45
  • @AndersonGreen: glad to hear it! Feel free to upvote my answer or press the "check" button to accept it as the right one. – maerics Jan 15 '12 at 17:54
0

I would recommend Doug Crockfords JSON-java library. This allows you to convert json to native JAVA objects.

Zee Spencer
  • 3,328
  • 6
  • 27
  • 31
0

I would simply use json-lib and parse the array that way. for example see How to parse a JSON and turn its values into an Array?

Community
  • 1
  • 1
Artemiy
  • 1,929
  • 13
  • 18