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

猜你喜欢

转载自blog.csdn.net/yaomingyang/article/details/80252258