I have a javascript file fun.js
function fun1(){
var arr =['This','is','from','js'];
return arr;
}
I want to get this array in java array so I used nashorn as-
try{
ScriptEngine engine= new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("fun.js"));
Invocable invocable = (Invocable) engine;
Object obj = (Object)invocable.invokeFunction("fun1");
System.out.println(obj.toString());
}
catch(Exception e){
e.printStackTrace();
}
But I am getting this output- [object Array]
How can I get this output as java array?
public static void main(String[] args) throws JSONException, FileNotFoundException, ScriptException, NoSuchMethodException {
ScriptEngine engine= new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("C:\\Users\\Niku\\eclipse-workspace\\java_sample\\src\\test.js"));
Invocable invocable = (Invocable) engine;
Object obj = (Object)invocable.invokeFunction("fun1");
Gson gson = new Gson();
String k = gson.toJson(obj);
JSONObject o = new JSONObject(k);
System.out.println(o.getString("0"));
System.out.println(o.getString("1"));
System.out.println(o.getString("2"));
System.out.println(o.getString("3"));
Iterator x = o.keys();
JSONArray jsonArray = new JSONArray();
ArrayList<String> ar = new ArrayList<String>();
while (x.hasNext()){
String key = (String) x.next();
ar.add(o.get(key).toString());
jsonArray.put(o.get(key));
}
System.out.println(ar);
}
Iterating through the JsonObject can get you each element which can be added to another array to be used.