|
From: Jeff E. <jem...@fr...> - 2008-04-28 15:11:52
|
This should work:
Map map = (Map)(interp.eval("dictToJavaMap(getDictionary())").__tojava__(Map.class));
Heshan Suriyaarachchi wrote:
> On Fri, Apr 25, 2008 at 9:03 PM, Jeff Emanuel <jem...@fr...> wrote:
>
>> That's correct, unfortunately PyDictionary does not
>> implement java.util.Map.
>>
>> You need to copy the dictionary to a Java map before
>> passing it to Java code.
>>
>> def dictToJavaMap(dict):
>> import java
>> javaMap =java.util.HashMap()
>> for i in dict.items():
>> javaMap[i[0]]=i[1]
>> return javaMap
>>
>> Hi Jeff,
> I used the method above to convert the Dictionary to a Map. When
> the method returns a javaMap , I am collecting it using a PyObject (within
> my java code).Therefore I have to convert it to a Map.At that moment it
> gives a class cast exception. Is there an alternative way of doing this
> correctly?
>
>
> PySystemState.initialize();
> PythonInterpreter interp = new PythonInterpreter();
> interp.exec("import sys");
> interp.exec("print sys.path");
>
> String str3 =
> "/home/heshan/workspace/IdeaProjects/PYtest/src/essentialScripts/myfile.py";
> interp.execfile(str3);
> PyObject obj = interp.eval("getDictionary()");
>
> String str4 = "dictToJavaMap(" + obj + ")";
> PyObject obj2 = interp.eval(str4);
>
> //aDictionary[k].get('operationName')
> //String str = "" + obj + "[].get('operationName')";
> //interp.exec(str);
>
> Map map = (Map) obj2;
> /*
> The above line gives a exception like below
> Exception in thread "main" java.lang.ClassCastException:
> org.python.core.PyJavaInstance cannot be cast to java.util.Map
> */
> Iterator k = map.keySet().iterator();
> while (k.hasNext()) {
> String key = (String) k.next();
> System.out.println("Key " + key + "; Value " +
> (String) map.get(key));
> }
>
>
> Thanx in advance
>
>
> ------------------------------------------------------------------------
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
> Don't miss this year's exciting event. There's still time to save $100.
> Use priority code J8TL2D2.
> http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Jython-users mailing list
> Jyt...@li...
> https://lists.sourceforge.net/lists/listinfo/jython-users
|