在的Javascript我有以下代碼:如何從JSObject轉換爲地圖(反之亦然),或列出JSObject成員
var r=applet.foo({var0:99,var1:'foo',var2:applet});
在我的Java小程序,我有以下幾點:
public JSObject foo(JSObject args){
System.out.println("The function is correctly invoked");
//In fact, the following works perfectly:
System.out.println("var1 is:"+(String)args.getMember("var1"));
JSObject w=JSObject.getWindow(this);
JSObject j=(JSObject)w.eval("new Object();");
Map m=new Hashmap();
//TODO here all the keys and values of args should be added to m
m.put("hello","world");
//TODO here all the keys and values of m should be added to j
return j;
}
這怎麼辦? (待辦事項)
閱讀http://docstore.mik.ua/orelly/web/jscript/ch19_06.html,我發現那裏有一個getSlot方法JSObject,但如果我這樣做
args.getSlot(0)
我只有一個例外:
netscape.javascript.JSException: No such slot 0 on JavaScript object
...
爲了獲得密鑰,不需要定義'keys'方法。使用'eval'方法就足夠了,因爲它在'JSObject'的上下文中執行JavaScript代碼:'JSObject keys =(JSObject)args.eval(「Object.keys(this)」);' – Ezze 2012-12-19 18:25:36