Pedantic
Pedantic

Reputation: 1378

Format JSON String in JSP

I have started working on JSON, I am returning a JSON from JSP through AJAX call. Its working well.

only i need to change the format of my returning JSON String.

Following is the String what my JSP is returing.

[{"VV":0,"desc":"XXXXXXX","amount":0,"date":"12/03/2013","watch":""},{"VV":1,"desc":"XXXXXXX","amount":1,"date":"12/03/2013","watch":""}]

and Below is the String what I want my JSP to return.

{"total":"2","rows":[{"VV":0,"desc":"XXXXXXX","amount":0,"date":"12/03/2013","watch":""},{"VV":1,"desc":"XXXXXXX","amount":1,"date":"12/03/2013","watch":""}] }

Can Any one please help.

Following code I am using to send the output back to front end.

JSONArray arrayObj=new JSONArray();

JSONObject json = new JSONObject();
  json.put("VV", i);
  json.put("desc", "XXXXXXXXX");
  json.put("amount", 1);  
  json.put("date", "12/03/2013");
  json.put("watch", "");

PrintWriter out1 = response.getWriter();
  out1.println(arrayObj);

Upvotes: 0

Views: 975

Answers (2)

Shashi
Shashi

Reputation: 359

What you need to so with the "json" object that you have created is to allocate to a new variable. Something like this:

arrayObj.put(json);

Next you need to create another object:

JSONObject finalObj = new JSONObject();
finalObj.put("total", 2);
finalObj.put("rows",arrayObj);
finalObj.flush();

Upvotes: 1

user2094311
user2094311

Reputation:

I think you need to convert it to String and override the toString() method according to your needs and pass it to your JSP page

Upvotes: 0

Related Questions