map转换成字符串的方法

第一种:json-lib

依赖:


    net.sf.json-lib
    json-lib
    2.4
    jdk15

方法:

            Map map = new HashMap();
	        map.put("sex", "男");
	        JSONObject jsonObject = JSONObject.fromObject(map);
	        //将json对象转化为json字符串
	        String result = jsonObject.toString();

第二种:alibaba的fastJson(推荐)


    com.alibaba
    fastjson
    1.2.41

方法:

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

第三种:google的gson


    com.google.code.gson
    gson
    2.3.1

方法:

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

你可能感兴趣的:(Java开发)