问题描述
是否可以在Rhino中以JavaScript对象的形式访问Java映射和列表?
Is there a way to access Java Maps and Lists as JavaScript Objects in Rhino?
我有一个Map,它仅包含其他映射以及基元和字符串列表,我想将其传递给Rhino脚本并对其进行处理,然后将修改后的对象返回给Java-但由于它们是java.util.Map和java.util.List对象,我不能使用标准的JavaScript关联数组语法.即:fooMap.get("keyName")
将起作用,但fooMap.keyName
和fooMap["keyName"]
将不起作用.
I have a Map which contains only other maps and lists of primitives and Strings, I'd like to pass this to a Rhino script and do stuff to it, and return the modified object back out to Java - but since they are java.util.Map and java.util.List Objects, I can't use standard JavaScript associative array syntax.ie: fooMap.get("keyName")
will work, but fooMap.keyName
and fooMap["keyName"]
will not.
我不知道是否有特定于Rhino的方式来执行此操作,或者是否有某些转换/广播实用程序会有所帮助.Commons BeanUtils是不够的,因为要将Map转换为bean(可以通过关联数组语法进行访问),必须首先创建一个包含所有命名的mutators/accessor的类.我不会在运行时知道对象的结构.
I don't know if there is a Rhino-specific way to do this, or if there is some conversion/cast utility that will help.Commons BeanUtils is not sufficient, because to convert a Map to a bean (which can be accessed via associative array syntax), you must first create a class which has all of the named mutators/accessors. I won't know the structure of the object at runtime.
推荐答案
看看RingoJS.它具有方便的包装,用于Rhino的列表和地图,例如这个
Take a look at RingoJS. It has convenient wrappers for list and map for Rhino such as this one
这篇关于访问Java Maps&在Rhino中列为JavaScript物件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!