解決Asp.net Mvc返回JsonResult中DateTime類型數(shù)據(jù)格式問題的方法
來源:易賢網(wǎng) 閱讀:1169 次 日期:2016-08-08 14:31:43
溫馨提示:易賢網(wǎng)小編為您整理了“解決Asp.net Mvc返回JsonResult中DateTime類型數(shù)據(jù)格式問題的方法”,方便廣大網(wǎng)友查閱!

問題背景:

在使用asp.net mvc 結(jié)合jquery esayui做一個(gè)系統(tǒng),但是在使用使用this.json方法直接返回一個(gè)json對(duì)象,在列表中顯示時(shí)發(fā)現(xiàn)datetime類型的數(shù)據(jù)在轉(zhuǎn)為字符串是它默認(rèn)轉(zhuǎn)為Date(84923838332223)的格式,在經(jīng)過查資料發(fā)現(xiàn)使用前端來解決這個(gè)問題的方法不少,但是我又發(fā)現(xiàn)在使用jquery easyui時(shí),加載列表數(shù)據(jù)又不能對(duì)數(shù)據(jù)進(jìn)行攔截,進(jìn)行數(shù)據(jù)格式轉(zhuǎn)換之后再加載,后來發(fā)現(xiàn)可以通過自定義JsonResult實(shí)現(xiàn),認(rè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; }

  }

}

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

原來它也是使用這個(gè)方法進(jìn)行序列化的。我們就可以在這個(gè)地方先獲取到j(luò)son序列化之后的字符串!然后做寫“小動(dòng)作”,就ok了

下面我就定義了一個(gè)自己的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 = @"\\/Date\((\d+)\)\\/";

        MatchEvaluator matchEvaluator = new MatchEvaluator(this.ConvertJsonDateToDateString);

        Regex reg = new Regex(p);

        jsonString = reg.Replace(jsonString, matchEvaluator);

        response.Write(jsonString);

      }

    }

     /// <summary> 

    /// 將Json序列化的時(shí)間由/Date(1294499956278)轉(zhuǎn)為字符串 .

    /// </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;

    }

  }

在這里做的“小動(dòng)作”就是紅色部分,得到字符串以后,通過正則表達(dá)式的方式獲得Date(12347838383333)的字符串,然后把它轉(zhuǎn)換為DateTime類型,最后在轉(zhuǎn)為我們想要的格式即可,這個(gè)格式可以使用FormateStr屬性設(shè)置。

剩下的就是使用我們自己定義的JsonResult來替換asp.net mvc默認(rèn)的JsonResult的問題了,接著從源碼中找答案,下面是Controller類的部分代碼

protected internal JsonResult Json(object data)

    {

      return this.Json(data, null, null, JsonRequestBehavior.DenyGet);

    }

    protected internal JsonResult Json(object data, string contentType)

    {

      return this.Json(data, contentType, null, JsonRequestBehavior.DenyGet);

    }

    protected internal JsonResult Json(object data, JsonRequestBehavior behavior)

    {

      return this.Json(data, null, null, behavior);

    }

    protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding)

    {

      return this.Json(data, contentType, contentEncoding, JsonRequestBehavior.DenyGet);

    }

    protected internal JsonResult Json(object data, string contentType, JsonRequestBehavior behavior)

    {

      return this.Json(data, contentType, null, behavior);

    }

    protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)

    {

      return new JsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior };

    }

以上是Controller類來實(shí)例化JsonResult的所有代碼。我們只需寫一個(gè)BaseController類,重寫最后一個(gè)方法即可,然后我們自己的Controller在繼承BaseController即可

下面是BaseController類的部分代碼,我們?yōu)榉奖阕约簜€(gè)性化的需要又定義了兩個(gè)MyJosn的方法

/// <summary>

    /// 返回JsonResult

    /// </summary>

    /// <param name="data">數(shù)據(jù)</param>

    /// <param name="contentType">內(nèi)容類型</param>

    /// <param name="contentEncoding">內(nèi)容編碼</param>

    /// <param name="behavior">行為</param>

    /// <returns>JsonReuslt</returns>

    protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)

    {

      return new CustomJsonResult

      {

        Data = data,

        ContentType = contentType,

        ContentEncoding =contentEncoding,

        JsonRequestBehavior = behavior,

        FormateStr = "yyyy-MM-dd HH:mm:ss"

      };

    }

    /// <summary>

    /// 返回JsonResult.24     /// </summary>

    /// <param name="data">數(shù)據(jù)</param>

    /// <param name="behavior">行為</param>

    /// <param name="format">json中dateTime類型的格式</param>

    /// <returns>Json</returns>

    protected JsonResult MyJson(object data, JsonRequestBehavior behavior,string format)

    {

      return new CustomJsonResult

      {

        Data = data,

        JsonRequestBehavior = behavior,

        FormateStr = format

      };

    }

    /// <summary>

    /// 返回JsonResult42     /// </summary>

    /// <param name="data">數(shù)據(jù)</param>

    /// <param name="format">數(shù)據(jù)格式</param>

    /// <returns>Json</returns>

    protected JsonResult MyJson(object data, string format)

    {

      return new CustomJsonResult

      {

        Data = data,

        FormateStr = format

      };

    }

最后我們?cè)谧约旱腃ontroller中調(diào)用即可

public class ProjectMileStoneController : BaseController

  {

    /// <summary>

    /// 首頁視圖

    /// </summary>

    /// <returns>視圖</returns>

    public ActionResult Index()

    {

      return this.View();

    }

    #region 項(xiàng)目里程碑查詢

    /// <summary>

    /// 根據(jù)項(xiàng)目編號(hào)獲取項(xiàng)目里程碑

    /// </summary>

    /// <param name="projectId">項(xiàng)目編號(hào)</param>

    /// <returns>項(xiàng)目里程碑</returns>

    public JsonResult GetProjectMileStoneByProjectId(int projectId)

    {

      IList<ProjectMileStone> projectMileStones = FacadeContainer.Get<IProjectMileStoneService>().GetProjectMileStonesByProjectId(projectId);

      return this.MyJson(projectMileStones, "yyyy.MM.dd");

    }

    #endregion

  }

以上就是Asp.net Mvc返回JsonResult中DateTime類型數(shù)據(jù)格式問題的解決方法,希望對(duì)大家的學(xué)習(xí)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)