Casting Array Result From JavaScript to Something Useful Using ScriptObjectMirror

筅森魡賤 提交于 2019-12-22 17:59:13

问题


If I have a JS function that returns an array of strings, how should I go about casting it to a useful type (I'm thinking either a Java array or Collection class)? I've noticed that the return type is always a ScriptObjectMirror which has an interesting to(Class<?> clazz) method, but I'm uncertain on it's use. I've checked several other StackOverflow questions, but none were useful. Can you show an example?


回答1:


Ok this worked for me:

ScriptObjectMirror result = (ScriptObjectMirror) function.invokeFunction("nameGen", 10);
        String[] strings = result.to(String[].class);


来源:https://stackoverflow.com/questions/29452758/casting-array-result-from-javascript-to-something-useful-using-scriptobjectmirro

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