使用FastJSON 对Map/JSON/String 进行互相转换

如题:

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);

2.JSON转String

        JSONObject json = new JSONObject();
        json.put("username", "yaomy");
        json.put("password", "123");

        json.toJSONString();

3.JSON转Map

        JSONObject json = new JSONObject();
        json.put("username", "yaomy");
        json.put("password", "123");

        Map<String, Object> map = (Map<String, Object>)json;

4.String转JSON

        String str = "{\"username\":\"yaomy\",\"password\":\"123\"}";
        JSONObject json = JSONObject.parseObject(str);
原创文章 84 获赞 46 访问量 21万+

猜你喜欢

转载自blog.csdn.net/yyj108317/article/details/105137964