一个统一将数据转换为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(); } }

 

posted @ 2019-01-30 09:33  受访市民廖先生  阅读(544)  评论(0编辑  收藏  举报