Map對象轉換成Json格式的String字串

來源:互聯網
上載者:User

標籤:todo   引號   object   處理   rac   sys   exception   import   tostring   


 

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 block21 e.printStackTrace();22 } catch (JsonMappingException e) {23 // TODO Auto-generated catch block24 e.printStackTrace();25 } catch (IOException e) {26 // TODO Auto-generated catch block27 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格式的字串。

 

Map對象轉換成Json格式的String字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.