使用gson将字符串转换成对象

 Gson gson =  new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        System.out.println(gson.toJson(obj));
        System.out.println(obj.toString());
        BaseResultVo<PageDataVo<BrowseHistory>>  baseResultVo = gson.fromJson(gson.toJson(obj),new TypeToken<BaseResultVo<PageDataVo<BrowseHistory>>>(){}.getType());
        PageDataVo<BrowseHistory> pageDataVo = baseResultVo.getData();
        return pageDataVo;
		
输出的代码:
{"code":"200","msg":"success","data":{"start":0,"pageSize":1,"curPage":1,"totalPage":3,"totalRows":3,"dataList":[{"id":18,"siteId":0,"subId":1,"ip":"string","docId":1,"docLibId":1,"loginName":"string","createTime":"2018-07-02 19:32:33","updateTime":"2018-07-02 19:32:33"}]}}
{code=200, msg=success, data={start=0, pageSize=1, curPage=1, totalPage=3, totalRows=3, dataList=[{id=18, siteId=0, subId=1, ip=string, docId=1, docLibId=1, loginName=string, createTime=2018-07-02 19:32:33, updateTime=2018-07-02 19:32:33}]}}

 注意:

gson.toJson(obj) 此处不能换成obi.tostring(),否则出错,会报日期转换失败的错误
posted @ 2018-07-02 19:47  Andrew_F  阅读(2723)  评论(0编辑  收藏  举报