Map的转化
1.Map转JSON
Map<String,Object> param=new HashMap<>();
param.put("param",param);
param.put("a",1);
JSONObject jsonObject=new JSONObject(param);
System.out.println(jsonObject);
打印结果:
{"a":1,"param":"{a=1, param=(this Map)}"}
2.Map转String
添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
Map<String, Object> map = new HashMap<>();
map.put("a", "a");
map.put("b", "b");
String s = JSONObject.toJSONString(map);
System.out.println(s);
结果:
{"a":"a","b":"b"}
3JSON转map
JSONObject json = new JSONObject();
json.put("ccc", "321");
json.put("bbb", "123");
System.out.println(json);
Map<String, Object> map2 = (Map<String, Object>)json;
System.out.println(map2.toString());
结果:
{"ccc":"321","bbb":"123"}
{"ccc":"321","bbb":"123"}
4String转json
String str = "{\"username\":\"dsad\",\"qwewqe\":\"123\"}";
JSONObject json3 = JSONObject.parseObject(str);
System.out.println(json3);
结果:
{"qwewqe":"123","username":"dsad"}