map轉換成字符串的方法


第一種:json-lib

依賴:

<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
</dependency> 

方法:

            Map map = new HashMap();
	        map.put("sex", "男");
	        JSONObject jsonObject = JSONObject.fromObject(map);
	        //將json對象轉化為json字符串
	        String result = jsonObject.toString(); 

第二種:alibaba的fastJson(推薦)

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.41</version>
</dependency> 

方法:

            Map map = new HashMap();
	        map.put("sex", "男");
	        String result = JSONUtils.toJSONString(map); 

第三種:google的gson

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3.1</version>
</dependency> 

方法:

            Map map = new HashMap();
	        map.put("sex", "男");
	        String result = new Gson().toJson(param).toString();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM