Java Servlet生成Json格式数据

来源:互联网 发布:数据猿公司怎么样 编辑:程序博客网 时间:2024/05/21 04:16

http://blog.csdn.net/x_white/article/details/11770785


在Servlet中覆写doGet方法,是用JSONStringer 类:

[java] view plain copy
 print?
  1. protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  2.         throws ServletException, IOException {  
  3.     // TODO Auto-generated method stub  
  4.     String IdList = Dao.getAllTourId();  
  5.     String[] strID = IdList.split("#");  
  6.       
  7.     JSONStringer stringer = new JSONStringer();  
  8.     int tID;  
  9.     String tourName, tourList, tourIdList;  
  10.   
  11.     try {  
  12.         stringer.array();  
  13.         for(int i = 0; i < strID.length; i++) {  
  14.             tID = Integer.parseInt(strID[i]);  
  15.             tourName = Dao.getTourName(tID);  
  16.             tourList = Dao.getTourList(tID);  
  17.             tourIdList = Dao.getPlaceIdList(tID);  
  18.             stringer.object().key("tID").value(tID).  
  19.                 key("name").value(tourName).  
  20.                 key("tourList").value(tourList).  
  21.                 key("tourIDList").value(tourIdList).endObject();  
  22.         }  
  23.         stringer.endArray();  
  24.     } catch (JSONException e) {  
  25.         e.printStackTrace();  
  26.     } catch (Exception e) {  
  27.         e.printStackTrace();  
  28.     }  
  29.     resp.getOutputStream().write(stringer.toString().getBytes("UTF-8"));  
  30.     resp.setContentType("text/json; charset=UTF-8");  
  31. }  

如果其中是用了HashMap类, 则如下:

[java] view plain copy
 print?
  1. protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  2.         throws ServletException, IOException {  
  3.     // TODO Auto-generated method stub  
  4.       
  5.     String param = req.getParameter("param");  
  6.     TourManager tm = new TourManager();  
  7.     JSONStringer stringer = new JSONStringer();  
  8.       
  9.     if(param.equals("Place")) {  
  10.         HashMap mapPlace = tm.getPlace();  
  11.           
  12.         try {  
  13.             stringer.array();  
  14.               
  15.             stringer.object();  
  16.             Iterator it = mapPlace.keySet().iterator();  
  17.             while(it.hasNext()) {  
  18.                 Object key = it.next();  
  19.                 stringer.key((String)key).value(mapPlace.get(key));  
  20.             }  
  21.             stringer.endObject();  
  22.               
  23.             stringer.endArray();  
  24.         } catch (JSONException e) {  
  25.             // TODO Auto-generated catch block  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29.     else if(param.equals("Tour")) {  
  30.         HashMap mapTour = tm.getTours();  
  31.           
  32.         try {  
  33.             stringer.array();  
  34.               
  35.             stringer.object();  
  36.             Iterator it = mapTour.keySet().iterator();  
  37.             while(it.hasNext()) {  
  38.                 Object key = it.next();  
  39.                 stringer.key((String)key).value(mapTour.get(key));  
  40.             }  
  41.             stringer.endObject();  
  42.             stringer.endArray();  
  43.         } catch (JSONException e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.       
  48.     resp.getOutputStream().write(stringer.toString().getBytes("UTF-8"));  
  49.     resp.setContentType("text/json; charset=UTF-8");  

最后是用resp将数据写入返回
0 0
原创粉丝点击