一个统一将数据转换为JSON的方法
这是我得方法:
导包:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public void writeJson(Object obj) { HttpServletResponse response = ServletActionContext.getResponse(); JSONArray jsonArray = JSONArray.fromObject(obj); response.setContentType("text/javascript; charset=utf-8"); try { response.getWriter().write(jsonArray.toString()); jsonArray = null; } catch (Exception e) { e.printStackTrace(); } } public void writeJsonObject(Object obj) { HttpServletResponse response = ServletActionContext.getResponse(); JSONObject jsonObject = JSONObject.fromObject(obj); response.setContentType("text/javascript; charset=utf-8"); try { response.getWriter().write(jsonObject.toString()); jsonObject = null; } catch (Exception e) { e.printStackTrace(); } }
别人的方法:
导包:
import net.sf.json.JSONArray;
public void sendToForwardByList( List<SupplierAchieve> list ) { JSONArray jobj = JSONArray.fromObject( list ); response.setCharacterEncoding( "utf-8" ); try { response.getWriter().write( jobj.toString() ); jobj = null; list = null; } catch( IOException e ) { e.printStackTrace(); } }