为什么java(nashnorn库)没有正确地计算js代码?

rpppsulh  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(334)

我有一些代码可以在javascript ide中很好地工作,比如webstorm,但是当我尝试使用nashorn库(在java程序中运行js)将其移植到java时,会出现以下错误:“name:undefined,age:undefined”(根据这里的错误,可能还会有其他代码跟随)
下面是完整的代码。下面的这种编码方法似乎在一般情况下是有效的,但是在有些情况下,我并没有得到预期的结果。
如有任何帮助或建议,我们将不胜感激。
谢谢!

public class JavaScriptEvaluationExample
{
    public static void main(String[] args) throws ScriptException, NoSuchMethodException
    {
        // Basic JavaScript evaluation
        String json = "{\n" +
                "    \"name\": \"John\",\n" +
                "    \"age\": 30,\n" +
                "    \"cars\": [\n" +
                "        {\n" +
                "            \"name\": \"Ford\",\n" +
                "            \"models\": [\n" +
                "                \"Fiesta\",\n" +
                "                \"Focus\",\n" +
                "                \"Mustang\"\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"BMW\",\n" +
                "            \"models\": [\n" +
                "                \"320\",\n" +
                "                \"X3\",\n" +
                "                \"X5\"\n" +
                "            ]\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"Fiat\",\n" +
                "            \"models\": [\n" +
                "                \"500\",\n" +
                "                \"Panda\"\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        String jsScript = "function parseJSONFields(param)\n" +
                "{\n" +
                "    var result = \"\";\n" +
                "    var carName =\"\" ;\n" +
                "    var models = \"\";\n" +
                "    var name = param.name + \",\";\n" +
                "    var age = param.age;\n" +
                "    result = \"Name: \" + name + \" Age: \" + age + \"\\n\";\n" +
                "    for (item in param.cars)\n" +
                "    {\n" +
                "        result  += \" \"+ \" >> \" + param.cars[item].name +\": \"\n" +
                "        result+= \" \"  + param.cars[item].models\n" +
                "        result += \"\\n\";\n" +
                "    }\n" +
                "    return result;\n" +
                "}";

        System.out.println(runJS("parseJSONFields", json, jsScript));
    }

    // ------------------------------------------------------------------------------------------------
    private static Object runJS(String functionName, String param, String script) throws ScriptException, NoSuchMethodException
   // ------------------------------------------------------------------------------------------------
    {
        Object resultString = "";
        String errorResultString = "";

        ScriptEngine js = null;
        try
        {
            if (param.length() > 0)
            {
                js = new ScriptEngineManager().getEngineByName("javascript");
                js.eval(script);
                Invocable inv = (Invocable) js;
            }
        }
        catch (Exception e)
        {
            errorResultString = e.getMessage() + " ---> " + e.getMessage();
        }

        return errorResultString.length() > 0 ? errorResultString
                : ((Invocable) js).invokeFunction(functionName, param);
    }
}
hrysbysz

hrysbysz1#

感谢所有回复的人。
问题是,正如第一个人所指出的,我忘记了用json.parse()语句将字符串转换为json对象。
再次非常感谢。

相关问题