I'm trying to use javax.script.ScriptEngine to eval() some JS scripts.
How can I know if the result after eval() which is of type Object is an array? And if so, how can I cast it?
Right now, to know if the object is of type Number or of type String i use instanceof. And when the result of the script is an array, if I print with System.out.println() the object returned it simply prints [object Array].
As you noticed, it's not java array but a javascript array, if you print the class of the return object, you probably will find it's "ScriptObjectMirror". I have a work around for this which is toString the array in another your script variable, then get the value of that (see below example). I believe there is a better way to resolve this array issue, waiting the good answer too.
engine.eval("var fruits = [\"Banana\", \"Orange\", \"Apple\", \"Mango\"]; var d = fruits.toString();");
System.out.println(engine.get("fruits").getClass());
System.out.println(engine.get("d").getClass());
System.out.println(engine.get("d"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With