犀牛:如何从个ScriptableObject获得所有属性?
-
23-09-2019 - |
题
我使用的是Javascript对象与配置属性的对象。 例如。我在JavaScript此目的:
var myProps = {prop1: 'prop1', prop2: 'prop2', 'prop3': 'prop3'};
此目的(NativeObject)返回到我在Java函数。 E.g。
public Static void jsStaticFunction_test(NativeObject obj) {
//work with object here
}
我想从它那里得到的对象,并构建HashMap的所有属性。
任何帮助将受到赞赏。
解决方案
好了,如果你仔细看,你会看到,NativeObject实现Map接口,所以你可能会与NativeObject工作非常出色....但要回答你的问题:你可能已经使用了常用的方法为获得任何映射的键 - 值对
for (Entry<Object, Object> e : obj.entrySet()){
mapParams.put(e.getKey().toString(), e.getValue().toString());
}
一个投就已经足以让你的情况,因为你只有字符串作为值。所以,如果你真的想要一个HashMap:
HashMap<String, String> mapParams = new HashMap<String, String>((Map<String,String>)obj); //if you wanted a HashMap
但是,如果你只是想一个普通的地图,它更简单,少占用RAM:
Map<String, String> mapParams = (Map<String,String>)obj;
其他提示
所以,我解决我的问题:)
代码:
public static void jsStaticFunction_test(NativeObject obj) {
HashMap<String, String> mapParams = new HashMap<String, String>();
if(obj != null) {
Object[] propIds = NativeObject.getPropertyIds(obj);
for(Object propId: propIds) {
String key = propId.toString();
String value = NativeObject.getProperty(obj, key).toString();
mapParams.put(key, value);
}
}
//work with mapParams next..
}
不隶属于 StackOverflow