怎么把Java Pojo转换成Nashorn Json? [英] How to convert Java Pojo to Nashorn Json?
本文介绍了怎么把Java Pojo转换成Nashorn Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Java对象,我想将其转换为json对象并传递给Nashorn javascript引擎.谷歌为此很难找到答案!有人可以告诉我该怎么做吗?
I have a Java object that I want to turn into a json object and pass to the Nashorn javascript engine. It is surprisingly difficult to google an answer for this! Can someone tell me how to do it?
我尝试过:
ObjectMapper mapper = new ObjectMapper();
String inputModelAsString = mapper.writeValueAsString(inputModel);
然后将字符串json传递给函数:
And then passing the string json to the function:
result = invocable.invokeFunction(PROGRAM_FUNCTION, moduleName, inputModelAsString);
但是它是作为字符串而不是作为json传递的.
But it was passed as a string, not as a json.
推荐答案
您可以通过
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
ScriptObjectMirror json = (ScriptObjectMirror) engine.eval("("+inputModelAsString+")");
然后您可以在代码中传递json对象
Then you can pass the json object in you code
result = invocable.invokeFunction(PROGRAM_FUNCTION, moduleName, json);
这篇关于怎么把Java Pojo转换成Nashorn Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文