java 获取MongoDB的数据并包装成Json对象返回

java 获取MongoDB的数据并包装成Json对象返回

 

 

Map map=new HashMap();
		List zsjkzl_list = new ArrayList();
		QueryBuilder queryBuilder_zl = new QueryBuilder();
		queryBuilder_zl.put("SQDBH").is(numberLy.getMzh_zyh()).put("SJLY").is(numberLy.getSjly());
		
		if (zlevent.getBgdbh() != null & !"".equals(zlevent.getBgdbh())) {
			queryBuilder_zl.put("XH").is(zlevent.getBgdbh());
		}

		DBObject dbObjectzl = queryBuilder_zl.get();
		DBCursor dbcursor_zl = collectionzlsqjgd.find(dbObjectzl);

		while (dbcursor_zl.hasNext()) {
			Map zlsqjgdmap = (HashMap) dbcursor_zl.next();
			ZsjkZl zsjkzl = new ZsjkZl();
			zsjkzl.setYydm(yydm);
			zsjkzl.setSfzh(sfzhm);
			zsjkzl.setYydm(yydm);

			zsjkzl.setJzjlbs(numberLy.getMzh_zyh());
			zsjkzl.setZlglbs(bgdbh);
			zsjkzl.setJzjlbs(zlsqjgdmap.get("DYBH") == null ? ""
					: zlsqjgdmap.get("DYBH").toString());
			zsjkzl.setZldmxxh(zlsqjgdmap.get("XH") == null ? ""
					: zlsqjgdmap.get("XH").toString());
			zsjkzl.setZldmxxmmc(zlsqjgdmap.get("ZLXMMC") == null ? ""
					: zlsqjgdmap.get("ZLXMMC").toString());
			zsjkzl.setZlglbs(zlsqjgdmap.get("SQDBH") == null ? ""
					: zlsqjgdmap.get("SQDBH").toString());
			zsjkzl_list.add(zsjkzl);
		}
		map.put("zsjkzl", zsjkzl_list);
		
		PrintWriter out = null;
		try {
			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/xml; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			out = response.getWriter();
			out.println(JSONArray.fromObject(map));
			out.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

 

你可能感兴趣的:(java 获取MongoDB的数据并包装成Json对象返回)