By using JavaScript APIs in Java 7, I am able to compile and invoke JavaScript functions. The issue is with value returned by the JavaScript function. Simple types can be type-cast easily. However, if a JavaScript function returns an object. How to convert the returned object into json string?
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine se = mgr.getEngineByName("JavaScript");
if (se instanceof Compilable) {
Compilable compiler = (Compilable) se;
CompiledScript script = compiler
.compile("function test() { return { x:100, y:200, z:150 } }; test();");
Object o = script.eval();
System.out.println(o);
} else {
System.out.println("Engine cann't compile code.");
}
How to convert object returned by JavaScript to JSON string?