1 //action处理页面发来的String,put到map转换成Json格式的String字符串
2 @RequestMapping("/seleteOaOrder")
3 @ResponseBody
4 public Object seleteOaOrder(String param){//param = "小明";
5 System.out.println("+++++++++++++++++param:"+param);
6 if(param!=null){
7 RequestEntity request = new RequestEntity();
8 request.setUserName("0755pss001");
9 request.setPwd("pss");
10 Map<String,String> map = new HashMap<String, String>();
11 map.put("applicant", param);
12 System.out.println("++++++++++++++++++转json之前的map对象:"+map.toString());
13 ObjectMapper json = new ObjectMapper();
14 String params = null;
15 try {
16 //把map对象转成json格式的String字符串
17 params = json.writeValueAsString(map);
18 System.out.println("++++++++++++++++++转成的json格式字符串:"+params);
19 } catch (JsonGenerationException e) {
20 // TODO Auto-generated catch block
21 e.printStackTrace();
22 } catch (JsonMappingException e) {
23 // TODO Auto-generated catch block
24 e.printStackTrace();
25 } catch (IOException e) {
26 // TODO Auto-generated catch block
27 e.printStackTrace();
28 }
29 request.setParams(params);
30 ResponseEntity response = buyOrderService.seleteOaOrder(request);
31 return response.getResult();
32 }
33 return null;
34 }
后台打印的结果:
转json之前的map对象:{applicant:小明}
转成的json格式字符串:{"applicant":"小明"};
在new Map对象的时候可以写成Map<String,Integer> map = new HashMap<String, Integer>();
这里param是一个int,等于2,
转json之前的map对象:{applicant:2}
转成的json格式字符串:{"applicant":2};这里param不会有双引号。
主要是用ObjectMapper(
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;)
对map对象写成JSON格式的字符串。