fastjson是阿里巴巴团队开发的一款JSON库,首先我们下载对应的版本: [fastjson](http://mvnrepository.com/artifact/com.alibaba/fastjson) 将下载到的jar包放入环境变量中,接下来介绍几种常见的用法:
1.Map转JSON
Map<String, Object> map = new HashMap<String, Object>();
map.put("username", "yaomy");
map.put("password", "123");
JSONObject json = new JSONObject(map);
1
2
3
4
5
2.JSON转String
JSONObject json = new JSONObject();
json.put("username", "yaomy");
json.put("password", "123");
json.toJSONString();
1
2
3
4
5
3.JSON转Map
JSONObject json = new JSONObject();
json.put("username", "yaomy");
json.put("password", "123");
Map<String, Object> map = (Map<String, Object>)json;
1
2
3
4
5
4.String转JSON
String str = "{\"username\":\"yaomy\",\"password\":\"123\"}";
JSONObject json = JSONObject.parseObject(str);
---------------------
作者:随风yy
来源:CSDN
原文:https://blog.csdn.net/yaomingyang/article/details/80252258
版权声明:本文为博主原创文章,转载请附上博文链接!