main方法代码:
import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Main { public static void main(String[] args) throws ScriptException { ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine jsEngine = scriptEngineManager.getEngineByName("js"); String jsTxt = " function getObjectJsonString() { " + " var man = new Object(); " + " man.name = 'zs'; " + " man.age = '18'; " + " var json_string = JSON.stringify(man);" + " return json_string;" + " }" + "var rs = getObjectJsonString();" ; jsEngine.eval(jsTxt); ScriptContext jsContext = jsEngine.getContext(); Object rs = jsContext.getAttribute("rs", ScriptContext.ENGINE_SCOPE); System.out.println(rs); } }
控制台输出:
标签:java,jdk,script,javascript,rs,import,jsEngine,javax,man From: https://www.cnblogs.com/changrunwei/p/17706677.html