3

In Action:

//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 File :

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

I am getting empty result when I see in JSP console

Where I went wrong? Thanks.

Roman C
  • 49,761
  • 33
  • 66
  • 176
sasi
  • 4,192
  • 4
  • 28
  • 47

1 Answers1

2

You are overcomplicating it :)

The Struts2-JSON-Plugin will serialize in JSON your Action's objects for you, so you don't need (and it's an error) to encode them by yourself.

Then, keeping your code and config as it is, just change the Action to:

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

/* Getter and Setter */

//In Action method   
jObj1 = new HashMap<String,Integer>();
jObj1.put("cid",232);
return Action.SUCCESS
Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • @Ligios , thanks for replying.. the actual scenario is..am getting json from the webservice as String(DTO objects List) i want to return it to the jsp .. how come i do that.. – sasi Apr 11 '14 at 17:23
  • Use json interceptor and stream result, it's in the link on my answer, in Roman C's answer – Andrea Ligios Apr 11 '14 at 21:00