赞
踩
Struts2中list转换为json
所需jar包,如下图:
实现代码,这里以execute方法为例:
@Override
public String execute() throws Exception {
//1.调用Service根据typecode获得数据字典对象list
List<BaseDict> list = baseDictService.getListByTypeCode(dict_type_code);
//2.将list转换为json格式
String json = JSONArray.fromObject(list).toString();
//3.设置json的编码格式并且将json发送给浏览器
ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
ServletActionContext.getResponse().getWriter().write(json);
return null;//告诉struts2不需要进行结果处理
}
结果:
[{"dict_enable":"1","dict_id":"6","dict_item_code":"","dict_item_name":"电话营销","dict_memo":"","dict_sort":1,"dict_type_code":"002","dict_type_name":"客户信息来源"},{"dict_enable":"1","dict_id":"7","dict_item_code":"","dict_item_name":"网络营销","dict_memo":"","dict_sort":2,"dict_type_code":"002","dict_type_name":"客户信息来源"}]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。