程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> ASP.NET MVC解決Json DataGrid返回的日期格式是/Date(20130450000365)

ASP.NET MVC解決Json DataGrid返回的日期格式是/Date(20130450000365)

編輯:關於ASP.NET

實際上是Json格式化問題,我們應該在返回json的時候進行格式化,我們需要重寫系統的JsonResult類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Script.Serialization;
namespace App.Common
{
    public class ToJsonResult : JsonResult
    {
        const string error = "該請求已被封鎖,因為敏感信息透露給第三方網站,這是一個GET請求時使用的。為了可以GET請求,請設置JsonRequestBehavior AllowGet。";
        /// <summary>
        /// 格式化字符串
        /// </summary>
        public string FormateStr
        {
            get;
            set;
        }
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
                String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException(error);
            }
    
            HttpResponseBase response = context.HttpContext.Response;
    
            if (!String.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                response.ContentType = "application/json";
            }
            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }
            if (Data != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string jsonstring = serializer.Serialize(Data);
    
    
                //string p = @"\\/Date\((\d+)\+\d+\)\\/";
    
                string p = @"\\/Date\(\d+\)\\/";
    
                MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
    
                Regex reg = new Regex(p);
    
                jsonstring = reg.Replace(jsonstring, matchEvaluator);
                response.Write(jsonstring);
            }
        }
    
        /// <summary>
        /// 將Json序列化的時間由/Date(1294499956278+0800)轉為字符串
        /// </summary>
        private string ConvertJsonDateToDateString(Match m)
        {
    
            string result = string.Empty;
    
            string p = @"\d";
            var cArray = m.Value.ToCharArray();
            StringBuilder sb = new StringBuilder();
    
            Regex reg = new Regex(p);
            for (int i = 0; i < cArray.Length; i++)
            {
                if (reg.IsMatch(cArray[i].ToString()))
                {
                    sb.Append(cArray[i]);
                }
            }
            // reg.Replace(m.Value;
    
            DateTime dt = new DateTime(1970, 1, 1);
    
            dt = dt.AddMilliseconds(long.Parse(sb.ToString()));
    
            dt = dt.ToLocalTime();
    
            result = dt.ToString("yyyy-MM-dd HH:mm:ss");
    
            return result;
    
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved