java 动态执行js,返回json

ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
engine.put("v", "{\"v1\":\"1s\",\"v2\":2}");
String js="function getResult(){ v=JSON.parse(v);return v;}";
//定义数学函数
engine.eval(js);
//取得调用接口
Invocable jsInvoke = (Invocable) engine;
//定义加法函数
Object result1 = jsInvoke.invokeFunction("getResult");
ScriptObjectMirror r= (ScriptObjectMirror)result1;
System.out.println(r.get("v1"));

你可能感兴趣的:(javascript,java,开发语言)