国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

解決Asp.net Mvc返回JsonResult中DateTime類型數(shù)據(jù)格式問題的方法

2024-07-10 12:47:10
字體:
來源:轉載
供稿:網(wǎng)友

問題背景:

           在使用asp.net mvc 結合jquery esayui做一個系統(tǒng),但是在使用使用this.json方法直接返回一個json對象,在列表中顯示時發(fā)現(xiàn)datetime類型的數(shù)據(jù)在轉為字符串是它默認轉為Date(84923838332223)的格式,在經過查資料發(fā)現(xiàn)使用前端來解決這個問題的方法不少,但是我又發(fā)現(xiàn)在使用jquery easyui時,加載列表數(shù)據(jù)又不能對數(shù)據(jù)進行攔截,進行數(shù)據(jù)格式轉換之后再加載,后來發(fā)現(xiàn)可以通過自定義JsonResult實現(xiàn),認為這種方法比較可行,就開始研究

我們先來看看jsonResult的源碼

public class JsonResult : ActionResult  {    public JsonResult()    {      this.JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.DenyGet;    }        public override void ExecuteResult(ControllerContext context)    {      if (context == null)      {        throw new ArgumentNullException("context");      }      if ((this.JsonRequestBehavior == System.Web.Mvc.JsonRequestBehavior.DenyGet) && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))      {        throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);      }      HttpResponseBase response = context.HttpContext.Response;      if (!string.IsNullOrEmpty(this.ContentType))      {        response.ContentType = this.ContentType;      }      else      {        response.ContentType = "application/json";      }      if (this.ContentEncoding != null)      {        response.ContentEncoding = this.ContentEncoding;      }      if (this.Data != null)      {        JavaScriptSerializer serializer = new JavaScriptSerializer();        response.Write(serializer.Serialize(this.Data));      }    }        public Encoding ContentEncoding { get; set; }        public string ContentType { get; set; }        public object Data { get; set; }        public System.Web.Mvc.JsonRequestBehavior JsonRequestBehavior { get; set; }  }}

當我看到上面代碼中的紅色部分,我感到有些熟悉,心里比較高興,以前使用過ashx來傳json的都應該用過此方法吧

原來它也是使用這個方法進行序列化的。我們就可以在這個地方先獲取到json序列化之后的字符串!然后做寫“小動作”,就ok了

下面我就定義了一個自己的JsonResult了

/// <summary>  /// 自定義Json視圖  /// </summary>  public class CustomJsonResult:JsonResult  {    /// <summary>    /// 格式化字符串    /// </summary>    public string FormateStr    {      get;      set;    }    /// <summary>    /// 重寫執(zhí)行視圖    /// </summary>    /// <param name="context">上下文</param>    public override void ExecuteResult(ControllerContext context)    {      if (context == null)      {        throw new ArgumentNullException("context");      }      HttpResponseBase response = context.HttpContext.Response;      if (string.IsNullOrEmpty(this.ContentType))      {        response.ContentType = this.ContentType;      }      else      {        response.ContentType = "application/json";      }      if (this.ContentEncoding != null)      {        response.ContentEncoding = this.ContentEncoding;      }      if (this.Data != null)      {        JavaScriptSerializer jss = new JavaScriptSerializer();        string jsonString = jss.Serialize(Data);        string p = @"http:///Date/((/d+)/)///";        MatchEvaluator matchEvaluator = new MatchEvaluator(this.ConvertJsonDateToDateString);        Regex reg = new Regex(p);        jsonString = reg.Replace(jsonString, matchEvaluator);        response.Write(jsonString);      }    }     /// <summary>     /// 將Json序列化的時間由/Date(1294499956278)轉為字符串 .    /// </summary>     /// <param name="m">正則匹配</param>    /// <returns>格式化后的字符串</returns>    private string ConvertJsonDateToDateString(Match m)    {      string result = string.Empty;      DateTime dt = new DateTime(1970, 1, 1);      dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));      dt = dt.ToLocalTime();      result = dt.ToString(FormateStr);      return result;    }  }            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿坝| 安福县| 鄂伦春自治旗| 山阴县| 同江市| 丰宁| 化州市| 龙山县| 原平市| 昭苏县| 舟山市| 花莲市| 正阳县| 荔浦县| 阿拉善右旗| 文化| 福州市| 宜春市| 外汇| 临潭县| 泸溪县| 龙南县| 锡林浩特市| 清流县| 章丘市| 临猗县| 福州市| 恩平市| 武乡县| 安义县| 楚雄市| 内丘县| 荥阳市| 华宁县| 石嘴山市| 治多县| 慈溪市| 天全县| 鲜城| 长治县| 韩城市|