fastJson和gson使用,字符串和实体以及map的相互转化

public class JSON {

    /**
     * 实体类转字符串
     */
    @Test
    public void testObjToJsonstring(){
        //方法1
        User user=new User();
        user.setId(1L);
        user.setName("张三");
        user.setAge("18");
        user.setCreateDate(new Date());

        String jsonstring = JSONObject.toJSONString(user);
        System.out.println(jsonstring);

        //方法2
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        String jsonstring2=gson.toJson(user);
        System.out.println(jsonstring2);

    }

    /**
     * JSON转字符串
     */
    @Test
    public void testJSONToJsonstring(){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id","1L");
        jsonObject.put("name","张三");
        jsonObject.put("age","16");
        System.out.println(JSONObject.toJSONString(jsonObject));

        //方法2
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        String jsonstring2=gson.toJson(jsonObject);
        System.out.println(jsonstring2);
    }


    /**
     * Map转字符串
     */
    @Test
    public void testMapToJsonstring(){
        Map<String,String> map = new HashMap<String,String>();
        map.put("key1","1111");
        map.put("key2","2222");
        map.put("key3","3333");
        map.put("key4","4444");
        System.out.println(map.toString());
        String jsonObject = JSONObject.toJSONString(map);
        System.out.println(jsonObject);

        //方法2
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        String jsonstring2=gson.toJson(map);
        System.out.println(jsonstring2);

    }

    /**
     * Map转json
     */
    @Test
    public void testMapToJson(){
        Map<String,Object> mapSum = Maps.newHashMap();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("key1","1111");
        map.put("key2","2222");
        map.put("key3","3333");
        map.put("key4","4444");
        mapSum.put("sum",map);
        JSONObject jsonObject=new JSONObject(mapSum);
        System.out.println(jsonObject.get("sum"));


    }

    /**
     * 字符串转实体类
     */
    @Test
    public void testJsonstringToobject(){
        String jsonString="{\"age\":\"18\",\"createDate\":\"2019-07-11 09:35:09\",\"id\":1,\"name\":\"张三\"}";
        User user = JSONObject.parseObject(jsonString, User.class);
        System.out.println(user.getAge());
        System.out.println(user.getCreateDate());
        System.out.println(user.getId());
        System.out.println(user.getName());
    }

    /**
     * 字符串转JSON
     */
    @Test
    public void testJSONStringToJson(){
        String jsonString="{\"age\":\"18\",\"createDate\":\"2019-07-11 09:35:09\",\"id\":1,\"name\":\"张三\"}";
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        System.out.println(jsonObject.get("age"));
        System.out.println(jsonObject.get("createDate"));
        System.out.println(jsonObject.get("id"));
        System.out.println(jsonObject.get("name"));
    }


    /**
     * 字符串转JSONArray
     */
    @Test
    public void testJSONStringTo字符串转JSONArray(){
        String jsonString2="[{\"age\":\"18\",\"createDate\":\"2019-07-11 09:35:09\",\"id\":1,\"name\":\"张三\"},{\"age\":\"19\",\"createDate\":\"2019-07-11 09:35:09\",\"id\":1,\"name\":\"李四\"}]";
        JSONArray array = JSONArray.parseArray(jsonString2);
        System.out.println(array.toJSONString());

        List<User> userList = JSONArray.parseArray(jsonString2,User.class);
        for (User user : userList) {
            System.out.println(user.getName());
        }
    }
    

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值