2017-07-11 23 views
-3

我在Python函数是返回一个JSON格式的词典:转换的Python解释为JSON格式JAVA

def getResults(): 
    investigationResults = {"One": "ResultOne"} 
    results = json.dumps(investigationResults, ensure_ascii=False) 
    return results 

我打电话使用Jython解释

PyObject pyObject = pythonInterpreter.eval("repr(Investigation().getResults())"); 

从Java这个功能尝试使用对象映射类将返回的pyObject转换为JSON对象:

ObjectMapper mapper = new ObjectMapper(); String jsonInString = mapper.writeValueAsString(pyObject);

但我无法将返回的pyObject转换为JSON对象,然后解析结果。 任何帮助表示赞赏。

+0

请详细说明你有什么样的麻烦。 (我们不接受显示为“gimme teh codez”的问题。) –

+0

当你使用json.dumps()时,你会得到一个字符串。你没有得到一个“JSON”对象。在python中,它们在加载时是字典,在转储时是字符串。 – xNinjaKittyx

+2

为什么你有'repr(...)'?摆脱它。 –

回答

0

谢谢COLDSPEED。删除repr(..)的伎俩。

PyObject pyObject = interpreter.eval("Investigation().getResults()"); 

    try{ 
     JSONObject jObject = new JSONObject(String.valueOf(pyObject)); 
     Iterator<?> keys = jObject.keys(); 

     while(keys.hasNext()){ 
      String key = (String)keys.next(); 
      System.out.println(jObject.get(key)); 
     } 
    }catch (Exception e){ 
     System.out.println(e); 
    }