日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

通过自己定义MVC的Controller的Json转换器解决日期序列化格式问题

發(fā)布時(shí)間:2025/6/15 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过自己定义MVC的Controller的Json转换器解决日期序列化格式问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

今日,在MVC框架下使用EasyUI的datagrid載入數(shù)據(jù)時(shí),服務(wù)端返回的Json日期格式為?/Date(1433088000000+0800)/ 。須要client進(jìn)一步轉(zhuǎn)換。并且也不符合EasyUI經(jīng)常使用的日期格式要求,為此,對(duì)MVC框架下的Controller做了一些研究。發(fā)現(xiàn)通過對(duì)Controller的Json方法進(jìn)行擴(kuò)展就能夠解決該問題。并且能夠通過進(jìn)一步自己定義序列化類,滿足不論什么類型的數(shù)據(jù)的序列化格式要求。

要實(shí)現(xiàn)該目標(biāo),須要完畢三個(gè)步驟的工作:

1、創(chuàng)建Controller的派生類。引入自己定義JsonResult

2、創(chuàng)建JsonResult的派生類。實(shí)現(xiàn)Json日期格式的自己定義實(shí)現(xiàn)

3、全部須要實(shí)現(xiàn)自己定義日期序列化格式的控制器,需繼承于上述Controller的派生類

詳細(xì)代碼實(shí)現(xiàn)示比例如以下:

/// <summary> /// 通過重載ExecuteResult方法,實(shí)現(xiàn)自己定義序列化日期的實(shí)現(xiàn) /// </summary> public class VMEJsonResult : JsonResult {public override void ExecuteResult(ControllerContext context){if (context == null){throw new ArgumentNullException("context");}HttpResponseBase response = context.HttpContext.Response;if (this.Data != null){JsonSerializerSettings setting = new JsonSerializerSettings();// 設(shè)置日期序列化的格式setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";response.Write(JsonConvert.SerializeObject(Data, setting));}} }/// <summary> /// 通過創(chuàng)建Controller的派生類來引入自己定義的Json實(shí)現(xiàn) /// </summary> public class VMEController : Controller {protected override JsonResult Json(object data, string contentType, Encoding contentEncoding){return new VMEJsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding };}public new JsonResult Json(object data, JsonRequestBehavior jsonRequest){return new VMEJsonResult { Data = data, JsonRequestBehavior = jsonRequest };}public new JsonResult Json(object data){return new VMEJsonResult { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet };} }/// <summary> /// 全部須要實(shí)現(xiàn)自己定義日期序列化效果的控制器。必須繼承于VMEController /// </summary> public class CouponController : VMEController {public ActionResult Index(){return View();}public ActionResult GetAllCouponTypes(){Hashtable hashtable = new Hashtable();hashtable["sessionId"] = "";string json = JsonHelper.Serialize(hashtable);string retJson = HttpHelper.PostForJson("http://localhost/vme", "CouponService.svc", "GetAllCouponTypes", json);string jsonResult = JsonHelper.GetString(retJson);List<CouponType> results = JsonHelper.DeserializeObject<List<CouponType>>(jsonResult);return Json(results, JsonRequestBehavior.AllowGet);} }



總結(jié)

以上是生活随笔為你收集整理的通过自己定义MVC的Controller的Json转换器解决日期序列化格式问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。