完整代码下载地址http://download.csdn.net/detail/woshishui6501/5306500
代码就不贴了核心就是要自己拼凑个json格式
package com.pbc.action;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pbc.bean.V9News;
import com.pbc.service.Grid_Service;
public class Grid_Action extends ActionSupport {
public String Grid_All() {
try{
Grid_Service grid_service=new Grid_Service();
List vdata=grid_service.get_data();
int tCount=vdata.size();
JSONObject jsonContext = new JSONObject();
jsonContext.put("totalCount", 22);
JSONArray context = new JSONArray();
if(tCount!=0){
for(int i=0;i<tCount;i++){
V9News v9News=(V9News)vdata.get(i);
JSONObject cell = new JSONObject();
cell.put("id",1);
cell.put("title",v9News.getDescription());
context.add(cell);
}
}
jsonContext.put("results", context);
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/json; charset=gbk");
response.setCharacterEncoding("gbk");
PrintWriter printWriter=response.getWriter();
printWriter.write(jsonContext.toString());
}catch(Exception e){
e.printStackTrace();
return null;
}
return null;
}
}