從json數組到ArrayList
Gson gson = new Gson(); Car cars = gson.fromJson(result,new TypeToken<ArrayList<Car>>() {}.getType());
從實體類到JSON字符串
Gson gson = new Gson(); String jsonBDID = gson.toJson(bdPushID);
如何讓你傳遞出來的對象數據時間顯示正確
如果我們通過id來對數據庫進行查詢。那么你返回給前台的數據是有問題的。
問題數據:
這里顯示的時間不對 "cretime": "2018-07-04T12:47:55.000+0000", "updatetime": "2018-07-04T12:47:55.000+0000",
轉換方式1
@ResponseBody @RequestMapping(value = "/spaceinfo", method = RequestMethod.POST) public Result getSpaceObjInfo(@RequestParam("spaceid") String spaceid) { YksptSpace space = yksptSpaceService.selectById(spaceid); // 稍微轉換一下,已解決時間的問題 String spacejson = JSON.toJSONString(space); JSONObject resultObj = JSON.parseObject(spacejson); return Result.ok().put("result", spacejson); }
轉換方式2,在你的bean里面給你的時間,加上一個json注解
@JsonSerialize對javabean進行json格式化 @JsonSerialize(using=JsonDateSerializer.class) public Date getModtime() { return modtime; }
這時候返回的數據就會顯示正確
"cretime": 1530708475000,
"updatetime": 1530708475000,
dao.xml層次轉換毫秒方法
CONCAT( UNIX_TIMESTAMP(startday), '000' ) AS startday
dao.xml層次sql語句做if判斷
<where> 1 = 1 <if test="classid !=null "> and CONCAT(p.classid) regexp #{classid} <!--多條件查詢寫法:調用StringUtils工具包正則轉換classid = StringUtil.preRegStr(classid); !列如 前端傳classid:1;2;3 --!> </if> <if test="status==4 or status==null or status==''"> and p.status = 05 </if> <if test="usertype =='jdleader.user' or usertype =='jdorg.user'"> and r.creusertype = #{usertype} </if> <if test="pname!=null and pname!=''"> and pname LIKE CONCAT('%',#{pname},'%') </if> <if test="startday!=null and startday!=''"> and startday <= #{startday} <!-- 前端Body參數 --!> </if> <if test="endday!=null and endday!=''"> and endday >= #{endday} </if> </where> ORDER BY startday DESC<!-- where 判斷之后做排序 --!>
后台日期轉換:
@InitBinder public void initBinder(WebDataBinder binder, WebRequest request) { //轉換日期 DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor為自定義日期編輯器 }
修改日期返回前端為毫秒:
String spacejson = JSON.toJSONString(page); JSONObject resultObj = JSON.parseObject(spacejson); sql寫法 CONCAT(UNIX_TIMESTAMP(f.cretime),'000') AS cretime