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();
原文地址:https://www.cnblogs.com/javalinux/p/14330912.html