1. 程式人生 > >json陣列,前後端傳值問題,與data時間轉毫秒

json陣列,前後端傳值問題,與data時間轉毫秒

從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 &lt;= #{startday} <!-- 前端Body引數 --!>
</if>
<if test="endday!=null and endday!=''">
and endday &gt;= #{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