Rhino JSON.stringify没有定义

时间:2016-02-16 19:22:28

标签: javascript java json maven rhino

我正在尝试使用maven中的 Rhino 1.7.7.1 从我的javascript中打印一个简单的JSON。

public static void main(String[] args) throws JsonProcessingException {
    Context context = Context.enter();
    try {
        ScriptableObject scope = context.initStandardObjects();
        System.out.println(context.evaluateString(scope, "JSON.stringify({a:1})", null, 1, null));
    } finally {
        Context.exit();
    }
}

但我有:

Exception in thread "main" org.mozilla.javascript.EcmaError: ReferenceError: "JSON" is not defined. (unnamed script#1)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3654)
    at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3632)
    at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3717)
    at org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1692)
    at org.mozilla.javascript.gen.c1._c0(unnamed script:1)
    at org.mozilla.javascript.gen.c1.call(unnamed script)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:398)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3065)
    at org.mozilla.javascript.gen.c1.call(unnamed script)
    at org.mozilla.javascript.gen.c1.exec(unnamed script)
    at org.mozilla.javascript.Context.evaluateString(Context.java:1104)
    at mypackage.RhinoService.main(RhinoService.java:34)

1 个答案:

答案 0 :(得分:3)

在Rhino 1.7引擎中没有实现JSON.stringify。请参阅Rhino compatibility table