Java Servlet生成Json格式数据

Java Servlet生成Json格式数据

分类: Web JAVA2013-09-17 14:38 4805人阅读 评论(1) 收藏 举报

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

[java] view plaincopyprint?

  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 plaincopyprint?

  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");  


转载于:https://my.oschina.net/u/1049180/blog/335787

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值