[Serializable] public class JsonResult { private StatusMessageType _messageType; private string _messageContent; /// <summary> /// /// </summary> /// <param></param> /// <param></param> /// <param></param> public JsonResult(StatusMessageType messageType, string messageContent, object data = null) { _messageType = messageType; _messageContent = messageContent; Data = data; } public StatusMessageType MessageType { get { return _messageType; } set { _messageType = value; } } public string MessageContent { get { return _messageContent; } set { _messageContent = value; } } public object Data { get; set; } public string ToJson() { JavaScriptSerializer serializer = new JavaScriptSerializer(); var json = serializer.Serialize(this); //string p = @"\\/Date(\d+)\\/"; //MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString); //Regex reg = new Regex(p); //json = reg.Replace(json, matchEvaluator); return json; } private static 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("d"); return result; } }
StatusMessageType枚举类:
/// <summary> /// 提示消息类别 /// </summary> public enum StatusMessageType { /// <summary> /// 权限不足 /// </summary> NoAccess = -2, /// <summary> /// 错误 /// </summary> Error = -1, /// <summary> /// 成功 /// </summary> Success = 1, /// <summary> /// 提示信息 /// </summary> Hint = 0 }
其他的扩展方法就不一一给出来了。