Json--后台将List集合转换成json数据

如若转载请注明出处:http://blog.csdn.net/qq_20011175/article/details/76723928
前后端分离式开发无需返回值跳转,直接使用response输出json数据
 这种开发方式减少了前后端开发者的冲突。

控制层:
 
public class ReportAction {
	private static final long serialVersionUID = 1L;
	private IReportService reportService;

	public ReportAction() {
		reportService = ServiceFactory.getReportInstance();
	}

	public void getReportJson() {
		String jsonResult = reportService.getReportByDeptno();
		System.out.println(jsonResult);
		try {
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.println(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Service业务层:

@Overridepublic String getReportByDeptno() {
String jsonResult = null;
try {
List<Report> reportList = reportDao.getReportByDeptno();
//目标:通过GSON将该集合变成json格式的字符串,所谓结果
returnGson gson = new Gson();
jsonResult = gson.toJson(reportList, new TypeToken<List<Report>>(){}.getType());
} 
catch (Exception e) 
{
e.printStackTrace();
}
return jsonResult;
}

    原文作者:鱼香肉丝2016
    原文地址: https://blog.csdn.net/qq_20011175/article/details/76723928
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞