using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Web.Script.Serialization; using System.IO; using System.Runtime.Serialization; using System.Reflection; using System.Text.RegularExpressions; namespace EMIS.Utility { public class JsonHelper { //对应JSON的singleInfo成员 public string singleInfo = string.Empty; protected string _error = string.Empty; protected bool _success = true; protected long _totalCount = 0; protected System.Collections.ArrayList arrData = new ArrayList(); protected System.Collections.ArrayList arrDataItem = new ArrayList(); public JsonHelper() { } public static string ToJSON(object obj) { JavaScriptSerializer serializer = new JavaScriptSerializer(); var json = serializer.Serialize(obj); string pattern = @"\\/Date\((\d+)\)\\/"; MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString); Regex reg = new Regex(pattern); return reg.Replace(json, matchEvaluator); } /// /// 将Json序列化的时间由/Date(1304931520336+0800)/转为字符串 /// private static string ConvertJsonDateToDateString(System.Text.RegularExpressions.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("yyyy-MM-dd HH:mm:ss"); return result; } public static string ToJSON(object obj, int recursionDepth) { JavaScriptSerializer serializer = new JavaScriptSerializer(); serializer.RecursionLimit = recursionDepth; return serializer.Serialize(obj); } public static T Deserialize(object obj) { JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Deserialize(obj.ToString()); } public static string ToJsonByValueType(object obj) { ArrayList items = new ArrayList(); PropertyInfo[] properties = obj.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (PropertyInfo p in properties) { if (p.PropertyType.IsValueType || p.PropertyType.Name.StartsWith("String")) { var value = p.GetValue(obj, null); if (value != null) { if (p.PropertyType.Name.StartsWith("String")) value = System.Web.HttpContext.Current.Server.UrlEncode(value.ToString()); items.Add(string.Format("{0}:\"{1}\"", p.Name, value)); } } } return string.Format("{{{0}}}", string.Join(",", items.ToArray())); } //对应于JSON的success成员 public bool success { get { return _success; } set { //如设置为true则清空error if (success) _error = string.Empty; _success = value; } } //对应于JSON的error成员 public string error { get { return _error; } set { //如设置error,则自动设置success为false if (value != "") _success = false; _error = value; } } public long totlalCount { get { return _totalCount; } set { _totalCount = value; } } //重置,每次新生成一个json对象时必须执行该方法 public void Reset() { _success = true; _error = string.Empty; singleInfo = string.Empty; arrData.Clear(); arrDataItem.Clear(); } public void AddItem(string name, string value) { arrData.Add(name + ":\"" + value + "\""); } public void AddItem(string name) { arrData.Add(name); } public void AddItem2(string name, string value) { arrData.Add(name + ":" + value); } public void ItemOk() { arrData.Add("
"); } public void AddItemChildren(string name, string value) { arrData.Add(name + ":'" + value + "'"); } //序列化JSON对象,得到返回的JSON代码 public override string ToString() { StringBuilder sb = new StringBuilder(); //sb.Append("{"); //sb.Append("totalCount:" + totlalCount.ToString() + ","); //sb.Append("success:" + _success.ToString().ToLower() + ","); //sb.Append("error:\"" + _error.Replace("\"", "\\\"") + "\","); //sb.Append("singleInfo:\"" + singleInfo.Replace("\"", "\\\"") + "\","); // sb.Append("data:["); sb.Append("["); int index = 0; sb.Append("{"); if (arrData.Count <= 0) { sb.Append("}]"); } else { foreach (string val in arrData) { index++; if (val != "
") { sb.Append(val + ","); } else { sb = sb.Replace(",", "", sb.Length - 1, 1); sb.Append("},"); if (index < arrData.Count) { sb.Append("{"); } } } sb = sb.Replace(",", "", sb.Length - 1, 1); sb.Append("]"); } //sb.Append("}"); return sb.ToString(); } //序列化JSON对象,得到返回的JSON代码 //返回子頁的JSON數據 public string ChildrenToString(string strtmp) { return "children:" + strtmp; } } }