Map、JSON、String之间的转换

使用alibaba的fastjson

1、Map转json

Map map = new HashMap();

JSONObject json = new JSONObject(map);

2、Map转String

Map map = new HashMap<>();

String s = JSONObject.toJSONString(map);

3、JSON转String

JSONObject json = new JSONObject();

json.toJSONString();

4、JSON转Map

JSONObject json = new JSONObject();

Map map = (Map)json;

5、String转json

String str = "{\"username\":\"dsad\",\"qwewqe\":\"123\"}";

JSONObject json = JSONObject.parseObject(str);

 

使用google

maven依赖



    com.google.code.gson
    gson
    2.3.1

1、Map转json

Map map = new HashMap(); 
String json=JSON.toJSONString(map); 
System.out.println(json); 
 

你可能感兴趣的:(后台,java)