Map 类型 转换为JSON类型

代码如下:

String current = request.getParameter("current");
		Map  a = request.getParameterMap();
		Iterator it = a.entrySet().iterator();
		System.out.println(request.getParameter("searchPhrase"));
		while(it.hasNext()){
			Entry e = (Entry) it.next();
			System.out.println(e.getKey() + ":" + e.getValue());
		}
		
		response.setContentType("text/json");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("GBK");
		PrintWriter out = response.getWriter();
		HashMap resultMap = new HashMap();
		resultMap.put("current", "1");
		resultMap.put("rowCount", "10");
		resultMap.put("total", "100");
		List result = new ArrayList();
		for (int i = 0; i < 11; i++) {
			Map temp = new HashMap();
			temp.put("id", i);
			temp.put("sender", "sender" + i + ":" + current);
			temp.put("received", "received" + i + ":" + current);
			result.add(temp);
		}
		resultMap.put("rows", result);
		ObjectMapper mapper = new ObjectMapper();
		SerializationConfig sc = mapper.getSerializationConfig();
		sc.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
		mapper.setSerializationConfig(sc);
		JsonGenerator gen = new JsonFactory().createJsonGenerator(out);
		mapper.writeValue(gen, resultMap);
		gen.close();
		out.flush();
		out.close();


你可能感兴趣的:(Map 类型 转换为JSON类型)