0

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?

1 Answer 1

1

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"));
Sign up to request clarification or add additional context in comments.

7 Comments

i can't find ScriptObjectMirror class. is it in an external json?
Its a class from jdk.nashorn.api.scripting package
ok but eclipse says me that jdk.nashorn.api.scripting.* is empty. do i need to download it from the market place? if yes what should i search?
do i need to install nashorn from the eclipse marketplace?
nashorn.jar is part of Java jdk 1.8.0_151
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.