从Struts2操作返回JSON对象提供空数据

时间:2014-04-02 06:58:30

标签: java json struts2

在行动中:

//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控制台中看到

时,我得到空的结果

我哪里出错了?感谢。

1 个答案:

答案 0 :(得分: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
相关问题