字符串转换为JSON格式,并传给前台
jar: json-20101018.jar 或者更高
JSONObject json =new JSONObject();
json.put("message", "删除成功");
response.setContentType("application/json;charset=UTF-8");
try {
response.getWriter().write(json .toString());
} catch (IOException e) {
e.printStackTrace();
}
对象转换为JSON格式,并传给前台
jar :jackson-mapper-asl-1.9.12.jar
Object obj =new Object();
StringWriter str =new StringWriter();
ObjectMapper mapper =new ObjectMapper();
mapper.setSerializationInclusion(Inclusion.NON_NULL);//忽略掉null值,不生成json
mapper.writeValue(str,obj);//把对象写入流中
response.setContentType("application/json;charset=UTF-8");
try {
response.getWriter().write(str .toString());
} catch (IOException e) {
e.printStackTrace();
}