【问题标题】:Returning JSON Object from Struts2 Action Giving Empty Data从 Struts2 动作返回 JSON 对象给空数据
【发布时间】:2014-05-13 06:44:01
【问题描述】:

在行动:

//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 配置文件

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

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

我哪里出错了?谢谢。

【问题讨论】:

标签: java json struts2


【解决方案1】:

你太复杂了:)

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

然后,保持您的代码和配置不变,只需将 Action 更改为:

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

/* Getter and Setter */

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

【讨论】:

  • @Ligios ,感谢您的回复..实际情况是..我从 web 服务获取 json 作为字符串(DTO 对象列表)我想将它返回给 jsp ..我怎么会这样做..
  • 使用 json 拦截器和流结果,它在我的答案的链接中,在 Roman C 的答案中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-25
  • 1970-01-01
  • 2020-10-13
  • 2017-08-31
  • 2020-03-14
  • 2016-01-23
相关资源
最近更新 更多