利用fastJson包实现json与对象互转

    @Test
    public void testGetAllUsers(){
        PageInfo pageInfo = userService.getAllUsers();
        //对象转json并美化
        String str = JSONObject.toJSONString(pageInfo,true);
        System.out.println("jsonStr:" + str);
        //json转对象
        System.out.println("PageInfo:" + JSONObject.parseObject(str,PageInfo.class));
    

输出:

jsonStr:{
    "endRow":2,
    "hasNextPage":false,
    "hasPreviousPage":false,
    "isFirstPage":true,
    "isLastPage":true,
    "list":[
        {
            "createdBy":"SYSTEM",
            "createdDate":1575545371000,
            "password":"123",
            "updatedBy":"SYSTEM",
            "updatedDate":1575545371000,
            "userId":1,
            "userName":"wcc"
        },
        {
            "createdBy":"SYSTEM",
            "createdDate":1575545631000,
            "password":"123",
            "updatedBy":"SYSTEM",
            "updatedDate":1575545631000,
            "userId":2,
            "userName":"魏灿灿"
        }
    ],
    "navigateFirstPage":1,
    "navigateLastPage":1,
    "navigatePages":8,
    "navigatepageNums":[1],
    "nextPage":0,
    "pageNum":1,
    "pageSize":1000,
    "pages":1,
    "prePage":0,
    "size":2,
    "startRow":1,
    "total":2
}

PageInfo:PageInfo{pageNum=1, pageSize=1000, size=2, startRow=1, endRow=2, total=2, pages=1, list=[{"password":"123","createdDate":1575545371000,"updatedBy":"SYSTEM","createdBy":"SYSTEM","updatedDate":1575545371000,"userName":"wcc","userId":1}, {"password":"123","createdDate":1575545631000,"updatedBy":"SYSTEM","createdBy":"SYSTEM","updatedDate":1575545631000,"userName":"魏灿灿","userId":2}], prePage=0, nextPage=0, isFirstPage=true, isLastPage=true, hasPreviousPage=false, hasNextPage=false, navigatePages=8, navigateFirstPage=1, navigateLastPage=1, navigatepageNums=[1]}

 

posted @ 2019-12-06 16:23  冷月孤影  阅读(1327)  评论(0编辑  收藏  举报