map转换成字符串的方法

时间:2021-01-27 13:33:47   收藏:0   阅读:0

第一种: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();
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!