Bonfra
Bonfra

Reputation: 296

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?

Upvotes: 0

Views: 150

Answers (1)

Mladen Savić
Mladen Savić

Reputation: 472

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"));

Upvotes: 1

Related Questions