using Newtonsoft.Json; using System; using System.Configuration; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Web.Configuration; using System.Web.Mvc; using System.Web.Script.Serialization; namespace WX.CRM.WebHelper { public class JsonHelper { public static T JsonDivertToObj(string json) { JavaScriptSerializer script = new JavaScriptSerializer(); T dic = script.Deserialize(json); return dic; } public static string ObjDivertToJson(object obj) { ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection; JavaScriptSerializer script = new JavaScriptSerializer(); script.MaxJsonLength = section != null ? section.MaxJsonLength : int.MaxValue; string json = script.Serialize(obj); return json; } } public class ConfigurableJsonResult : JsonResult { 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("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."); } 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(); ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection; if (section != null) { serializer.MaxJsonLength = section.MaxJsonLength; serializer.RecursionLimit = section.RecursionLimit; } else { serializer.MaxJsonLength = int.MaxValue; } var str = serializer.Serialize(Data); str = Regex.Replace(str, @"\\/Date\((\d+)\)\\/", match => { DateTime dt = new DateTime(1970, 1, 1); dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value)); dt = dt.ToLocalTime(); return dt.ToString("yyyy-MM-dd HH:mm:ss"); }); response.Write(str); } } } public class JsonNetResult : JsonResult { public Encoding ContentEncoding { get; set; } public string ContentType { get; set; } public object Data { get; set; } public JsonSerializerSettings SerializerSettings { get; set; } public Formatting Formatting { get; set; } public JsonNetResult(object data, Formatting formatting) : this(data) { Formatting = formatting; JsonRequestBehavior = JsonRequestBehavior.AllowGet; } public JsonNetResult(object data) : this() { Data = data; JsonRequestBehavior = JsonRequestBehavior.AllowGet; } public JsonNetResult() { Formatting = Formatting.None; SerializerSettings = new JsonSerializerSettings(); } public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); var response = context.HttpContext.Response; response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; if (ContentEncoding != null) response.ContentEncoding = ContentEncoding; if (Data == null) return; var writer = new JsonTextWriter(response.Output) { Formatting = Formatting }; var serializer = JsonSerializer.Create(SerializerSettings); serializer.Serialize(writer, Data); writer.Flush(); } } }