简体   繁体   中英

How can i extract an object from a javascript script in java

I have this code:

ScriptEngine INTERPRETER = new ScriptEngineManager().getEngineByName("js");
obj = INTERPRETER.eval("var obj = {a:1, b:2};");

So, from the java object obj how can i extract the 2 value form the js object?

You can use something like this:

 ScriptEngine INTERPRETER = new ScriptEngineManager().getEngineByName("js");
 ScriptObjectMirror obj = (ScriptObjectMirror) INTERPRETER.eval("obj = {a:1, b:2};");
 System.out.println(obj.get("b"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM