How to convert Java Pojo to Nashorn Json?

会有一股神秘感。 提交于 2019-12-06 08:53:18

You can convert json from engine by

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
ScriptObjectMirror json = (ScriptObjectMirror) engine.eval("("+inputModelAsString+")");

Then you can pass the json object in you code

result = invocable.invokeFunction(PROGRAM_FUNCTION, moduleName, json);

I faced a similar issue and handled it in a slightly different way.

I wouldn't access the class ScriptObjectMirror directly as it's part of the internal Nashorn's API and therefore prone to change.

You may try something like this:

engine.eval("var inputModel = " + inputModel + ";");    
Object json = engine.get("inputModel");
jatanp

you could use inbuild JSON feature in Nashorn as mentioned in

Nashorn JSON stringify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!