四个主要操作类:JsonConverter 、JsonHelper 、JsonSplit 、AjaxResult
一、JsonConverter:
自定义查询对象转换动态类、object动态类转换json包、json转换object动态类、DataReader转换为Json、DataSet转换为Json、DataTable转成Json、Datatable转换为Json 、格式化字符型日期型布尔型、过滤特殊字符等
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Linq; using System.Web.Script.Serialization; using System.Collections; namespace Common { public class JsonConverter { /// <summary> /// 自定义查询对象转换动态类 /// add yuangang by 2015-05-19 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static dynamic JsonClass(object obj) { return ConvertJson(Serialize(obj, true)); } /// <summary> /// object动态类转换json包 /// add yuangang by 2015-05-19 /// </summary> /// <param name="obj">对象</param> /// <param name="DateConvert">时间戳是否转换成日期类型</param> /// <returns></returns> public static string Serialize(object obj, bool DateConvert = false) { JavaScriptSerializer jss = new JavaScriptSerializer(); var str = jss.Serialize(obj); if (DateConvert) { str = System.Text.RegularExpressions.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"); }); } return str; } /// <summary> /// json转换object动态类 /// add yuangang by 2015-05-19 /// </summary> /// <param name="json"></param> /// <returns></returns> public static dynamic ConvertJson(string json) { JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); dynamic dy = jss.Deserialize(json, typeof(object)) as dynamic; return dy; } /// <summary> /// DataReader转换为Json /// </summary> /// <param name="dataReader">DataReader对象</param> /// <returns>Json字符串</returns> public static string ToJson(IDataReader dataReader) { try { StringBuilder jsonString = new StringBuilder(); jsonString.Append("["); while (dataReader.Read()) { jsonString.Append("{"); for (int i = 0; i < dataReader.FieldCount; i++) { Type type = dataReader.GetFieldType(i); string strKey = dataReader.GetName(i); string strValue = dataReader[i].ToString(); jsonString.Append(""" + strKey + "":"); strValue = StringFormat(strValue, type); if (i < dataReader.FieldCount - 1) { jsonString.Append(strValue + ","); } else { jsonString.Append(strValue); } } jsonString.Append("},"); } if (!dataReader.IsClosed) { dataReader.Close(); } jsonString.Remove(jsonString.Length - 1, 1); jsonString.Append("]"); if (jsonString.Length == 1) { return "[]"; } return jsonString.ToString(); } catch (Exception ex) { throw ex; } } /// <summary> /// DataSet转换为Json /// add yuangang by 2015-05-19 /// </summary> /// <param name="dataSet">DataSet对象</param> /// <returns>Json字符串</returns> public static string ToJson(DataSet dataSet) { string jsonString = "{"; foreach (DataTable table in dataSet.Tables) { jsonString += """ + table.TableName + "":" + ToJson(table) + ","; } jsonString = jsonString.TrimEnd(','); return jsonString + "}"; } /// <summary> /// DataTable转成Json /// add yuangang by 2015-05-19 /// </summary> /// <param name="jsonName"></param> /// <param name="dt"></param> /// <returns></returns> public static string ToJson(DataTable dt, string jsonName) { StringBuilder Json = new StringBuilder(); if (string.IsNullOrEmpty(jsonName)) jsonName = dt.TableName; Json.Append("{"" + jsonName + "":["); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { Json.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { Type type = dt.Rows[i][j].GetType(); Json.Append(""" + dt.Columns[j].ColumnName.ToString() + "":" + StringFormat(dt.Rows[i][j] is DBNull ? string.Empty : dt.Rows[i][j].ToString(), type)); if (j < dt.Columns.Count - 1) { Json.Append(","); } } Json.Append("}"); if (i < dt.Rows.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return Json.ToString(); } /// <summary> /// Datatable转换为Json /// add yuangang by 2015-05-19 /// </summary> /// <param name="table">Datatable对象</param> /// <returns>Json字符串</returns> public static string ToJson(DataTable dt) { StringBuilder jsonString = new StringBuilder(); jsonString.Append("["); DataRowCollection drc = dt.Rows; for (int i = 0; i < drc.Count; i++) { jsonString.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { string strKey = dt.Columns[j].ColumnName; string strValue = drc[i][j].ToString(); Type type = dt.Columns[j].DataType; jsonString.Append(""" + strKey + "":"); strValue = StringFormat(strValue, type); if (j < dt.Columns.Count - 1) { jsonString.Append(strValue + ","); } else { jsonString.Append(strValue); } } jsonString.Append("},"); } jsonString.Remove(jsonString.Length - 1, 1); jsonString.Append("]"); if (jsonString.Length == 1) { return "[]"; } return jsonString.ToString(); } /// <summary> /// 格式化字符型、日期型、布尔型 /// add yuangang by 2015-05-19 /// </summary> /// <param name="str"></param> /// <param name="type"></param> /// <returns></returns> private static string StringFormat(string str, Type type) { if (type != typeof(string) && string.IsNullOrEmpty(str)) { str = """ + str + """; } else if (type == typeof(string)) { str = String2Json(str); str = """ + str + """; } else if (type == typeof(DateTime)) { str = """ + str + """; } else if (type == typeof(bool)) { str = str.ToLower(); } else if (type == typeof(byte[])) { str = """ + str + """; } else if (type == typeof(Guid)) { str = """ + str + """; } return str; } /// <summary> /// 过滤特殊字符 /// add yuangang by 2015-05-19 /// </summary> /// <param name="s"></param> /// <returns></returns> public static string String2Json(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.Length; i++) { char c = s.ToCharArray()[i]; switch (c) { case '"': sb.Append("\""); break; case '\': sb.Append("\\"); break; case '/': sb.Append("\/"); break; case 'b': sb.Append("\b"); break; case 'f': sb.Append("\f"); break; case 'n': sb.Append("\n"); break; case 'r': sb.Append("\r"); break; case 't': sb.Append("\t"); break; case 'v': sb.Append("\v"); break; case ' ': sb.Append("\0"); break; default: sb.Append(c); break; } } return sb.ToString(); } public static string GetDataGridJsonByDataSet(DataSet ds, string totalProperty, string root) { return GetDataGridJsonByDataTable(ds.Tables[0], totalProperty, root); } public static string GetDataGridJsonByDataTable(DataTable dt, string totalProperty, string root) { StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("({"" + totalProperty + "":"" + dt.Rows.Count + "","); jsonBuilder.Append("""); jsonBuilder.Append(root); jsonBuilder.Append("":["); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { jsonBuilder.Append("""); jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append("":""); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append("","); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("},"); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("]"); jsonBuilder.Append("})"); return jsonBuilder.ToString(); } public static string GetTreeJsonByDataSet(DataSet ds) { return GetTreeJsonByDataTable(ds.Tables[0]); } public static string GetTreeJsonByDataTable(DataTable dataTable) { DataTable dt = FormatDataTableForTree(dataTable); StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("["); for (int i = 0; i < dt.Rows.Count; i++) { jsonBuilder.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { jsonBuilder.Append("'"); if (dt.Columns[j].ColumnName == "leaf") { string leafValue = dt.Rows[i][j].ToString(); if (!string.IsNullOrEmpty(leafValue)) { jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append("':'"); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append("',"); } else { jsonBuilder.Remove(jsonBuilder.Length - 1, 1); } } else if (dt.Columns[j].ColumnName == "customUrl") { jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append(":'"); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append("',"); } else { jsonBuilder.Append(dt.Columns[j].ColumnName); jsonBuilder.Append("':'"); jsonBuilder.Append(dt.Rows[i][j].ToString()); jsonBuilder.Append("',"); } } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("},"); } jsonBuilder.Remove(jsonBuilder.Length - 1, 1); jsonBuilder.Append("]"); return jsonBuilder.ToString(); } private static DataTable FormatDataTableForTree(DataTable dt) { DataTable dtTree = new DataTable(); dtTree.Columns.Add("id", typeof(string)); dtTree.Columns.Add("text", typeof(string)); dtTree.Columns.Add("leaf", typeof(string)); dtTree.Columns.Add("cls", typeof(string)); dtTree.Columns.Add("customUrl", typeof(string)); dtTree.AcceptChanges(); for (int i = 0; i < dt.Rows.Count; i++) { DataRow drTree = dtTree.NewRow(); drTree["id"] = dt.Rows[i]["id"].ToString(); drTree["text"] = dt.Rows[i]["text"].ToString(); if (dt.Rows[i]["leaf"].ToString() == "Y") { drTree["leaf"] = "true"; drTree["cls"] = "file"; } else { drTree["cls"] = "folder"; } drTree["customUrl"] = dt.Rows[i]["customUrl"].ToString(); dtTree.Rows.Add(drTree); } return dtTree; } } /// <summary> /// 动态JSON解析 /// add yuangang by 2015-05-19 /// </summary> public class DynamicJsonObject : System.Dynamic.DynamicObject { private IDictionary<string, object> Dictionary { get; set; } public DynamicJsonObject(IDictionary<string, object> dictionary) { this.Dictionary = dictionary; } public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result) { result = this.Dictionary[binder.Name]; if (result is IDictionary<string, object>) { result = new DynamicJsonObject(result as IDictionary<string, object>); } else if (result is ArrayList && (result as ArrayList) is IDictionary<string, object>) { result = new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary<string, object>))); } else if (result is ArrayList) { result = new List<object>((result as ArrayList).ToArray()); } return this.Dictionary.ContainsKey(binder.Name); } } /// <summary> /// 动态JSON转换 /// add yuangang by 2015-05-19 /// </summary> public class DynamicJsonConverter : JavaScriptConverter { public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) { if (dictionary == null) throw new ArgumentNullException("dictionary"); if (type == typeof(object)) { return new DynamicJsonObject(dictionary); } return null; } public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) { throw new NotImplementedException(); } public override IEnumerable<Type> SupportedTypes { get { return new System.Collections.ObjectModel.ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) })); } } } }
二、JsonHelper:json的辅助类
using System.Collections.Generic; using System.Text; using Newtonsoft.Json; using System.Runtime.Serialization.Json; using System.IO; namespace Common { /// <summary> /// 提供了一个关于json的辅助类 /// </summary> public class JsonHelper { #region Method /// <summary> /// 类对像转换成json格式 /// </summary> /// <returns></returns> public static string ToJson(object t) { return JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include }); } /// <summary> /// 类对像转换成json格式 /// </summary> /// <param name="t"></param> /// <param name="HasNullIgnore">是否忽略NULL值</param> /// <returns></returns> public static string ToJson(object t, bool HasNullIgnore) { if (HasNullIgnore) return JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); else return ToJson(t); } /// <summary> /// json格式转换 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="strJson"></param> /// <returns></returns> public static T FromJson<T>(string strJson) where T : class { if (!strJson.IsNullOrEmpty()) return JsonConvert.DeserializeObject<T>(strJson); return null; } /// <summary> /// 功能描述:将List转换为Json /// </summary> /// <param name="a"></param> /// <returns></returns> public static string ListToJson(IList<object> a) { DataContractJsonSerializer json = new DataContractJsonSerializer(a.GetType()); string szJson = ""; //序列化 using (MemoryStream stream = new MemoryStream()) { json.WriteObject(stream, a); szJson = Encoding.UTF8.GetString(stream.ToArray()); } return szJson; } #endregion #region Property /// <summary> /// 数据状态 /// </summary> public string Status { get; set; } /// <summary> /// 提示信息 /// </summary> public string Msg { get; set; } /// <summary> /// 回传URL /// </summary> public string ReUrl { get; set; } /// <summary> /// 数据包 /// </summary> public object Data { get; set; } #endregion } }
三、JsonSplit:判断字符串是否为Json
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Common { /// <summary> /// 判断字符串是否为JSON /// </summary> public class JsonSplit { private static bool IsJsonStart(ref string json) { if (!string.IsNullOrEmpty(json)) { json = json.Trim('r', 'n', ' '); if (json.Length > 1) { char s = json[0]; char e = json[json.Length - 1]; return (s == '{' && e == '}') || (s == '[' && e == ']'); } } return false; } public static bool IsJson(string json) { int errIndex; return IsJson(json, out errIndex); } public static bool IsJson(string json, out int errIndex) { errIndex = 0; if (IsJsonStart(ref json)) { CharState cs = new CharState(); char c; for (int i = 0; i < json.Length; i++) { c = json[i]; if (SetCharState(c, ref cs) && cs.childrenStart)//设置关键符号状态。 { string item = json.Substring(i); int err; int length = GetValueLength(item, true, out err); cs.childrenStart = false; if (err > 0) { errIndex = i + err; return false; } i = i + length - 1; } if (cs.isError) { errIndex = i; return false; } } return !cs.arrayStart && !cs.jsonStart; } return false; } /// <summary> /// 获取值的长度(当Json值嵌套以"{"或"["开头时) /// </summary> private static int GetValueLength(string json, bool breakOnErr, out int errIndex) { errIndex = 0; int len = 0; if (!string.IsNullOrEmpty(json)) { CharState cs = new CharState(); char c; for (int i = 0; i < json.Length; i++) { c = json[i]; if (!SetCharState(c, ref cs))//设置关键符号状态。 { if (!cs.jsonStart && !cs.arrayStart)//json结束,又不是数组,则退出。 { break; } } else if (cs.childrenStart)//正常字符,值状态下。 { int length = GetValueLength(json.Substring(i), breakOnErr, out errIndex);//递归子值,返回一个长度。。。 cs.childrenStart = false; cs.valueStart = 0; //cs.state = 0; i = i + length - 1; } if (breakOnErr && cs.isError) { errIndex = i; return i; } if (!cs.jsonStart && !cs.arrayStart)//记录当前结束位置。 { len = i + 1;//长度比索引+1 break; } } } return len; } /// <summary> /// 字符状态 /// </summary> private class CharState { internal bool jsonStart = false;//以 "{"开始了... internal bool setDicValue = false;// 可以设置字典值了。 internal bool escapeChar = false;//以""转义符号开始了 /// <summary> /// 数组开始【仅第一开头才算】,值嵌套的以【childrenStart】来标识。 /// </summary> internal bool arrayStart = false;//以"[" 符号开始了 internal bool childrenStart = false;//子级嵌套开始了。 /// <summary> /// 【0 初始状态,或 遇到“,”逗号】;【1 遇到“:”冒号】 /// </summary> internal int state = 0; /// <summary> /// 【-1 取值结束】【0 未开始】【1 无引号开始】【2 单引号开始】【3 双引号开始】 /// </summary> internal int keyStart = 0; /// <summary> /// 【-1 取值结束】【0 未开始】【1 无引号开始】【2 单引号开始】【3 双引号开始】 /// </summary> internal int valueStart = 0; internal bool isError = false;//是否语法错误。 internal void CheckIsError(char c)//只当成一级处理(因为GetLength会递归到每一个子项处理) { if (keyStart > 1 || valueStart > 1) { return; } //示例 ["aa",{"bbbb":123,"fff","ddd"}] switch (c) { case '{'://[{ "[{A}]":[{"[{B}]":3,"m":"C"}]}] isError = jsonStart && state == 0;//重复开始错误 同时不是值处理。 break; case '}': isError = !jsonStart || (keyStart != 0 && state == 0);//重复结束错误 或者 提前结束{"aa"}。正常的有{} break; case '[': isError = arrayStart && state == 0;//重复开始错误 break; case ']': isError = !arrayStart || jsonStart;//重复开始错误 或者 Json 未结束 break; case '"': case ''': isError = !(jsonStart || arrayStart); //json 或数组开始。 if (!isError) { //重复开始 [""",{"" "}] isError = (state == 0 && keyStart == -1) || (state == 1 && valueStart == -1); } if (!isError && arrayStart && !jsonStart && c == ''')//['aa',{}] { isError = true; } break; case ':': isError = !jsonStart || state == 1;//重复出现。 break; case ',': isError = !(jsonStart || arrayStart); //json 或数组开始。 if (!isError) { if (jsonStart) { isError = state == 0 || (state == 1 && valueStart > 1);//重复出现。 } else if (arrayStart)//["aa,] [,] [{},{}] { isError = keyStart == 0 && !setDicValue; } } break; case ' ': case 'r': case 'n'://[ "a",rn{} ] case ' ': case 't': break; default: //值开头。。 isError = (!jsonStart && !arrayStart) || (state == 0 && keyStart == -1) || (valueStart == -1 && state == 1);// break; } //if (isError) //{ //} } } /// <summary> /// 设置字符状态(返回true则为关键词,返回false则当为普通字符处理) /// </summary> private static bool SetCharState(char c, ref CharState cs) { cs.CheckIsError(c); switch (c) { case '{'://[{ "[{A}]":[{"[{B}]":3,"m":"C"}]}] #region 大括号 if (cs.keyStart <= 0 && cs.valueStart <= 0) { cs.keyStart = 0; cs.valueStart = 0; if (cs.jsonStart && cs.state == 1) { cs.childrenStart = true; } else { cs.state = 0; } cs.jsonStart = true;//开始。 return true; } #endregion break; case '}': #region 大括号结束 if (cs.keyStart <= 0 && cs.valueStart < 2 && cs.jsonStart) { cs.jsonStart = false;//正常结束。 cs.state = 0; cs.keyStart = 0; cs.valueStart = 0; cs.setDicValue = true; return true; } // cs.isError = !cs.jsonStart && cs.state == 0; #endregion break; case '[': #region 中括号开始 if (!cs.jsonStart) { cs.arrayStart = true; return true; } else if (cs.jsonStart && cs.state == 1) { cs.childrenStart = true; return true; } #endregion break; case ']': #region 中括号结束 if (cs.arrayStart && !cs.jsonStart && cs.keyStart <= 2 && cs.valueStart <= 0)//[{},333]//这样结束。 { cs.keyStart = 0; cs.valueStart = 0; cs.arrayStart = false; return true; } #endregion break; case '"': case ''': #region 引号 if (cs.jsonStart || cs.arrayStart) { if (cs.state == 0)//key阶段,有可能是数组["aa",{}] { if (cs.keyStart <= 0) { cs.keyStart = (c == '"' ? 3 : 2); return true; } else if ((cs.keyStart == 2 && c == ''') || (cs.keyStart == 3 && c == '"')) { if (!cs.escapeChar) { cs.keyStart = -1; return true; } else { cs.escapeChar = false; } } } else if (cs.state == 1 && cs.jsonStart)//值阶段必须是Json开始了。 { if (cs.valueStart <= 0) { cs.valueStart = (c == '"' ? 3 : 2); return true; } else if ((cs.valueStart == 2 && c == ''') || (cs.valueStart == 3 && c == '"')) { if (!cs.escapeChar) { cs.valueStart = -1; return true; } else { cs.escapeChar = false; } } } } #endregion break; case ':': #region 冒号 if (cs.jsonStart && cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 0) { if (cs.keyStart == 1) { cs.keyStart = -1; } cs.state = 1; return true; } // cs.isError = !cs.jsonStart || (cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 1); #endregion break; case ',': #region 逗号 //["aa",{aa:12,}] if (cs.jsonStart) { if (cs.keyStart < 2 && cs.valueStart < 2 && cs.state == 1) { cs.state = 0; cs.keyStart = 0; cs.valueStart = 0; //if (cs.valueStart == 1) //{ // cs.valueStart = 0; //} cs.setDicValue = true; return true; } } else if (cs.arrayStart && cs.keyStart <= 2) { cs.keyStart = 0; //if (cs.keyStart == 1) //{ // cs.keyStart = -1; //} return true; } #endregion break; case ' ': case 'r': case 'n'://[ "a",rn{} ] case ' ': case 't': if (cs.keyStart <= 0 && cs.valueStart <= 0) //cs.jsonStart && { return true;//跳过空格。 } break; default: //值开头。。 if (c == '\') //转义符号 { if (cs.escapeChar) { cs.escapeChar = false; } else { cs.escapeChar = true; return true; } } else { cs.escapeChar = false; } if (cs.jsonStart || cs.arrayStart) // Json 或数组开始了。 { if (cs.keyStart <= 0 && cs.state == 0) { cs.keyStart = 1;//无引号的 } else if (cs.valueStart <= 0 && cs.state == 1 && cs.jsonStart)//只有Json开始才有值。 { cs.valueStart = 1;//无引号的 } } break; } return false; } } }
四、AjaxResult:前台Ajax请求的统一返回结果类
using System.Web.Script.Serialization; namespace Common { /// <summary> /// 前台Ajax请求的统一返回结果类 /// </summary> public class AjaxResult { private AjaxResult() { } private bool iserror = false; /// <summary> /// 是否产生错误 /// </summary> public bool IsError { get { return iserror; } } /// <summary> /// 错误信息,或者成功信息 /// </summary> public string Message { get; set; } /// <summary> /// 成功可能时返回的数据 /// </summary> public object Data { get; set; } #region Error public static AjaxResult Error() { return new AjaxResult() { iserror = true }; } public static AjaxResult Error(string message) { return new AjaxResult() { iserror = true, Message = message }; } #endregion #region Success public static AjaxResult Success() { return new AjaxResult() { iserror = false }; } public static AjaxResult Success(string message) { return new AjaxResult() { iserror = false, Message = message }; } public static AjaxResult Success(object data) { return new AjaxResult() { iserror = false, Data = data }; } public static AjaxResult Success(object data, string message) { return new AjaxResult() { iserror = false, Data = data, Message = message }; } #endregion /// <summary> /// 序列化 /// </summary> /// <returns></returns> public override string ToString() { return new JavaScriptSerializer().Serialize(this); } } }
常用的一些Json操作类,留着备用,需要的朋友的直接拿去用吧。
发表评论 取消回复