I have the following program, which executes Javascript in Java (nashorn) . The Javascript code is returning an object.
public Object execute(){
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine e = sem.getEngineByName("nashorn");
Invocable invocable = (Invocable)e;
ScriptEngineFactory f = e.getFactory();
Object result;
try {
String statement = "function fetch(value, count) { count++ ; return {'value': value,'count' : count} }; } ; ";
CompiledScript cs = ((Compilable)e).compile(statement);
cs.eval();
result = invocable.invokeFunction("fetch", 10,2);
}
catch (Exception se ) {
String version = System.getProperty("java.version");
System.out.println(version);
result = "script exception ";
}
How do I access the object values in my result object in Java?
Initially, I tried using result.toString() to get results. Seems to return [Object Object]
Is there a way, where I could return the results to result object such that I could get values equivalent to result.value , and result.count (similar to Javascript) .