本文介绍了Java:从ScriptEngine javascript返回一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用课程。以下是我想要做的一个简短示例:

I'm trying to evaluate javascript in Java by using the ScriptEngine class. Here is a short example of what I am trying to do:

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class Test {
    public static void main(String[] args) {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); //Creates a ScriptEngine
        Object obj = engine.eval("var obj = { value: 1 }; return obj; "); // Evals the creation of a simple object
        System.out.println(obj.value); // I get an invalid token error when trying to print a property of the object
    }
}

我很确定这个应该工作......但是我很难过,我会得到任何帮助。

I'm pretty sure that this should work... but I'm stumped, and I'll take any help I can get.

推荐答案

注意:以下内容适用于Java 8,使用引擎。

首先,让代码编译,从 println()语句中删除 .value obj 声明为对象对象不对没有字段。

First, to make the code compile, remove the .value from the println() statement. obj is declared to be type Object, and Object doesn't have a value field.

执行此操作后,运行代码时会出现以下异常:

Once you do that, you get the following exception when running the code:

Exception in thread "main" javax.script.ScriptException: <eval>:1:25 Invalid return statement
var obj = { value: 1 };  return obj;
                         ^ in <eval> at line number 1 at column number 25

这是因为你没有功能,所以你无法调用返回。脚本的返回值是最后一个表达式的值,所以只需说 obj

That is because you don't have a function, so you cannot call return. The return value of the script is the value of the last expression, so just say obj.

现在它将会运行并打印 [object Object] 。要查看您返回的对象类型,请更改为 println(obj.getClass()。getName())。这将打印。为方便起见,我已链接到javadoc。

Now it will run and print [object Object]. To see what type of object you got back, change to println(obj.getClass().getName()). That will print jdk.nashorn.api.scripting.ScriptObjectMirror. I've linked to the javadoc for your convenience.

ScriptObjectMirror 实现反过来实现 Map< String,Object> ,因此你可以调用 get(value)

ScriptObjectMirror implements Bindings which in turn implements Map<String, Object>, so you can call get("value").

工作代码是:

import javax.script.*;

public class Test {
    public static void main(String[] args) throws ScriptException {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
        Bindings obj = (Bindings)engine.eval("var obj = { value: 1 };  obj; ");
        Integer value = (Integer)obj.get("value");
        System.out.println(value); // prints: 1
    }
}

UPDATE

如何执行此操作的示例:

Example for how to do that:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import jdk.nashorn.api.scripting.ScriptObjectMirror;

public class Test {
    public static void main(String[] args) throws Exception {
        String script = "var f = {\n" +
                        "  value: 0,\n" +
                        "  add: function(n) {\n" +
                        "    this.value += n;\n" +
                        "    return this.value;\n" +
                        "  }\n" +
                        "};\n" +
                        "f; // return object to Java\n";
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
        ScriptObjectMirror obj = (ScriptObjectMirror)engine.eval(script);
        System.out.println("obj.value = " + obj.getMember("value"));
        System.out.println("obj.add(5): " + obj.callMember("add", 5));
        System.out.println("obj.add(-3): " + obj.callMember("add", -3));
        System.out.println("obj.value = " + obj.getMember("value"));
    }
}

OUTPUT

obj.value = 0
obj.add(5): 5.0
obj.add(-3): 2.0
obj.value = 2.0

这篇关于Java:从ScriptEngine javascript返回一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:12