2014-04-02 94 views
3

在行动:返回JSON对象,提供空数据

//Declaration 
    JSONObject jObj1 = null; 

    public JSONObject getjObj1() { 
      return jObj1; 
    } 
    public void setjObj1(JSONObject jObj1) { 
      this.jObj1 = jObj1; 
    } 

//In Action method 
String jsong="{\"cid\":232}"; 
jObj1 = new JSONObject(jsong); 

return Action.SUCCESS 

Struts的CFG文件

<action name="jsonAction" class="jsonAction" method="getJson"> 
      <result type="json" name="success"> 
       <param name="root">jObj1</param>     
      </result> 
     </action> 

我得到空的结果,当我在JSP控制台中看到

我哪里出错了?谢谢。

+0

你为什么不使用由S2提供的JSON插件? –

+0

[jQuery AJAX的可能的重复 - 问题返回JSON值](http://stackoverflow.com/questions/17093862/jquery-ajax-issue-returning-json-value) –

回答

2

你过于复杂吧:)

Struts2-JSON-Plugin will serialize in JSON your Action's objects for you,所以你并不需要(这是一个错误)自行进行编码。

然后,保持你的代码和配置,因为它是,只是改变了行动:

//Declaration 
Map<String,Integer> jObj1 = null; 

/* Getter and Setter */ 

//In Action method 
jObj1 = new HashMap<String,Integer>(); 
jObj1.put("cid",232); 
return Action.SUCCESS 
+0

你试过@sasi? –

+0

@Ligios,感谢您的回复..实际情况是..am从Web服务获取JSON作为字符串(DTO对象列表)我想将它返回给jsp ..我怎么这样做.. – sasi

+0

使用json拦截器和流结果,它在我的答案链接,罗马C的答案 –