2014-04-02 95 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的答案 –