有段时间在使用Ajax处理Json问题
虽然现在.net 3.0以后有了关于处理Json的类库
但是现在很多服务器还不支持
没办法有只有动手开始自己写了
- using System;
- using System.Reflection;
- using System.Collections;
- using System.Data;
- using System.Data.Common;
- /// <summary>
- /// Json转换工具
- /// 作者:吴奇凡
- /// 用于转换基本的对象
- /// </summary>
- public class FxJson
- {
- private FxJson()
- {
- }
- /// <summary>
- /// 对象转换为Json字符串
- /// </summary>
- /// <param name="jsonObject">对象</param>
- /// <returns>Json字符串</returns>
- public static string ToJsonAll(object jsonObject)
- {
- string jsonString = "{";
- PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties();
- for (int i = 0; i < propertyInfo.Length; i++)
- {
- object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
- string value = string.Empty;
- if (objectValue is DateTime || objectValue is string || objectValue is Guid || objectValue is TimeSpan)
- {
- value = "'" + objectValue + "'";
- }
- else if (objectValue is IEnumerable)
- {
- value = ToString((IEnumerable)objectValue);
- }
- else
- {
- value = objectValue.ToString();
- }
- jsonString += "/"" + propertyInfo[i].Name + "/":" + value + ",";
- }
- return FxJson.DeleteLast(jsonString) + "}";
- }
- /// <summary>
- /// 对象集合转换Json
- /// </summary>
- /// <param name="array">集合对象</param>
- /// <returns>Json字符串</returns>
- public static string ToString(IEnumerable array)
- {
- string jsonString = "[";
- foreach (object item in array)
- {
- jsonString += FxJson.ToJsonAll(item) + ",";
- }
- return FxJson.DeleteLast(jsonString) + "]";
- }
- /// <summary>
- /// 普通集合转换Json
- /// </summary>
- /// <param name="array">集合对象</param>
- /// <returns>Json字符串</returns>
- public static string ToArrayString(IEnumerable array)
- {
- string jsonString = "[";
- foreach (object item in array)
- {
- jsonString = item+",";
- }
- return FxJson.DeleteLast(jsonString) + "]";
- }
- /// <summary>
- /// 删除结尾字符
- /// </summary>
- /// <param name="str">需要删除的字符</param>
- /// <returns>完成后的字符串</returns>
- private static string DeleteLast(string str)
- {
- if (str.Length > 1)
- {
- return str.Substring(0, str.Length - 1);
- }
- return str;
- }
- /// <summary>
- /// Datatable转换为Json
- /// </summary>
- /// <param name="table">Datatable对象</param>
- /// <returns>Json字符串</returns>
- public static string ToString(DataTable table)
- {
- string jsonString = "[";
- DataRowCollection drc = table.Rows;
- for (int i = 0; i < drc.Count; i++)
- {
- jsonString += "{";
- foreach (DataColumn column in table.Columns)
- {
- jsonString += "/"" + column.ColumnName + "/":";
- if (column.DataType == typeof(DateTime) || column.DataType== typeof( string))
- {
- jsonString += "/"" + drc[i][column.ColumnName] + "/",";
- }
- else
- {
- jsonString += drc[i][column.ColumnName] + ",";
- }
- }
- jsonString = DeleteLast(jsonString) + "},";
- }
- return DeleteLast(jsonString) + "]";
- }
- /// <summary>
- /// DataReader转换为Json
- /// </summary>
- /// <param name="dataReader">DataReader对象</param>
- /// <returns>Json字符串</returns>
- public static string ToString(DbDataReader dataReader)
- {
- string jsonString = "[";
- while (dataReader.Read())
- {
- jsonString += "{";
- for (int i = 0; i < dataReader.FieldCount; i++)
- {
- jsonString += "/"" + dataReader.GetName(i) + "/":";
- if (dataReader.GetFieldType(i) == typeof(DateTime) || dataReader.GetFieldType(i) == typeof(string))
- {
- jsonString += "/"" + dataReader[i] + "/",";
- }
- else
- {
- jsonString += dataReader[i] + ",";
- }
- }
- jsonString = DeleteLast(jsonString) + "}";
- }
- dataReader.Close();
- return DeleteLast(jsonString) + "]";
- }
- /// <summary>
- /// DataSet转换为Json
- /// </summary>
- /// <param name="dataSet">DataSet对象</param>
- /// <returns>Json字符串</returns>
- public static string ToString(DataSet dataSet)
- {
- string jsonString = "{";
- foreach (DataTable table in dataSet.Tables)
- {
- jsonString += "/"" + table.TableName + "/":" + ToString(table) + ",";
- }
- return jsonString = DeleteLast(jsonString) + "}";
- }
- }
嘿嘿试试感觉不错哦
以下是测试记录
{"Fx_Users":[{"id":3,"userName":"aaaa","userPwd":"11111","realName":"管理员","us erState":0,"userRight":"1","phone1":"","phone2":"123","phone3":"123","mobile":"1 23","fax":"123","email":"123","msn":"123","qq":"123"},{"id":4,"userName":"user01 ","userPwd":"user01","realName":"普通用户","userState":0,"userRight":"2","phone1 ":"","phone2":"","phone3":"","mobile":"","fax":"","email":"","msn":"","qq":""}], "Fx_Messages":[{"id":2,"fromId":4,"toId":3,"title":"测试","msgText":"<P><EM><STR ONG><U>errewrewrdf</U></STRONG></EM></P><STRONG><EM><U>","sendDate":"2008/6/28 1 4:48:48","readDate":"2008/6/28 14:48:48","msgType":0,"msgState":0,"isFile":""},{ "id":3,"fromId":4,"toId":3,"title":"测试","msgText":"jdskfjadsklfjadlskfjkladsfj lkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfjads lkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkjasdl fjadsklfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:48","readDate":"2008/6/28 14:48:48","msgType":0,"msgState":0,"isFile":""},{"id":4,"fromId":4,"toId":3,"ti tle":"测试","msgText":"jdskfjadsklfjadlskfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjad slkfjasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfjadslkfjkladsfjlaskdfjlkadsjflkads jfkljadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkjasdlfjadsklfjalsdkjfladskfjla","se ndDate":"2008/6/28 14:48:48","readDate":"2008/6/28 14:48:48","msgType":0,"msgSta te":0,"isFile":""},{"id":5,"fromId":4,"toId":3,"title":"测试","msgText":"jdskfja dsklfjadlskfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasld kfjadsklfjladskfjadslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjflakd sfjakdsfjladsfkjasdlfjadsklfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:48"," readDate":"2008/6/28 14:48:48","msgType":0,"msgState":0,"isFile":""},{"id":6,"fr omId":4,"toId":3,"title":"测试","msgText":"jdskfjadsklfjadlskfjkladsfjlkadsjflkd sjfklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfjadslkfjkladsf jlaskdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkjasdlfjadsklfja lsdkjfladskfjla","sendDate":"2008/6/28 14:48:48","readDate":"2008/6/28 14:48:48" ,"msgType":0,"msgState":0,"isFile":""},{"id":7,"fromId":4,"toId":3,"title":"测试 ","msgText":"jdskfjadsklfjadlskfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlf kjasldkfjsadlkfjasldkfjadsklfjladskfjadslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslk fjadslkfjlakdsjflakdsfjakdsfjladsfkjasdlfjadsklfjalsdkjfladskfjla","sendDate":"2 008/6/28 14:48:48","readDate":"2008/6/28 14:48:48","msgType":0,"msgState":0,"isF ile":""},{"id":8,"fromId":4,"toId":3,"title":"测试","msgText":"jdskfjadsklfjadls kfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfj ladskfjadslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjl adsfkjasdlfjadsklfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:48","readDate": "2008/6/28 14:48:48","msgType":0,"msgState":0,"isFile":""},{"id":9,"fromId":4,"t oId":3,"title":"测试","msgText":"jdskfjadsklfjadlskfjkladsfjlkadsjflkdsjfklsdjkl fjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfjadslkfjkladsfjlaskdfjlk adsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkjasdlfjadsklfjalsdkjflads kfjla","sendDate":"2008/6/28 14:48:48","readDate":"2008/6/28 14:48:48","msgType" :0,"msgState":0,"isFile":""},{"id":10,"fromId":4,"toId":3,"title":"测试","msgTex t":"jdskfjadsklfjadlskfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkfj sadlkfjasldkfjadsklfjladskfjadslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkfj lakdsjflakdsfjakdsfjladsfkjasdlfjadsklfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:48","readDate":"2008/6/28 14:48:48","msgType":0,"msgState":0,"isFile":""}, {"id":11,"fromId":4,"toId":3,"title":"测试","msgText":"jdskfjadsklfjadlskfjklads fjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfja dslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkjas dlfjadsklfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:48","readDate":"2008/6/ 28 14:48:48","msgType":0,"msgState":0,"isFile":""},{"id":12,"fromId":4,"toId":3, "title":"测试","msgText":"jdskfjadsklfjadlskfjkladsfjlkadsjflkdsjfklsdjklfjdslfk jadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfjadslkfjkladsfjlaskdfjlkadsjflk adsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkjasdlfjadsklfjalsdkjfladskfjla", "sendDate":"2008/6/28 14:48:48","readDate":"2008/6/28 14:48:48","msgType":0,"msg State":0,"isFile":""},{"id":13,"fromId":4,"toId":3,"title":"测试","msgText":"jds kfjadsklfjadlskfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfj asldkfjadsklfjladskfjadslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjf lakdsfjakdsfjladsfkjasdlfjadsklfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:4 8","readDate":"2008/6/28 14:48:48","msgType":0,"msgState":0,"isFile":""},{"id":1 4,"fromId":4,"toId":3,"title":"测试","msgText":"jdskfjadsklfjadlskfjkladsfjlkads jflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfjadslkfjk ladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkjasdlfjads klfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:48","readDate":"2008/6/28 14:4 8:48","msgType":0,"msgState":0,"isFile":""},{"id":15,"fromId":4,"toId":3,"title" :"测试","msgText":"jdskfjadsklfjadlskfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjadslkf jasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfjadslkfjkladsfjlaskdfjlkadsjflkadsjfkl jadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkjasdlfjadsklfjalsdkjfladskfjla","sendDa te":"2008/6/28 14:48:48","readDate":"2008/6/28 14:48:48","msgType":0,"msgState": 0,"isFile":""},{"id":16,"fromId":4,"toId":3,"title":"测试","msgText":"jdskfjadsk lfjadlskfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfj adsklfjladskfjadslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfj akdsfjladsfkjasdlfjadsklfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:48","rea dDate":"2008/6/28 14:48:48","msgType":0,"msgState":0,"isFile":""},{"id":17,"from Id":4,"toId":3,"title":"测试","msgText":"jdskfjadsklfjadlskfjkladsfjlkadsjflkdsj fklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfjadslkfjkladsfjl askdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkjasdlfjadsklfjals dkjfladskfjla","sendDate":"2008/6/28 14:48:48","readDate":"2008/6/28 14:48:48"," msgType":0,"msgState":0,"isFile":""},{"id":18,"fromId":4,"toId":3,"title":"测试" ,"msgText":"jdskfjadsklfjadlskfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfk jasldkfjsadlkfjasldkfjadsklfjladskfjadslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkf jadslkfjlakdsjflakdsfjakdsfjladsfkjasdlfjadsklfjalsdkjfladskfjla","sendDate":"20 08/6/28 14:48:48","readDate":"2008/6/28 14:48:48","msgType":0,"msgState":0,"isFi le":""},{"id":19,"fromId":4,"toId":3,"title":"测试","msgText":"jdskfjadsklfjadls kfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfj ladskfjadslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjl adsfkjasdlfjadsklfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:48","readDate": "2008/6/28 14:48:48","msgType":0,"msgState":0,"isFile":""},{"id":20,"fromId":4," toId":3,"title":"测试","msgText":"jdskfjadsklfjadlskfjkladsfjlkadsjflkdsjfklsdjk lfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfjadslkfjkladsfjlaskdfjl kadsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkjasdlfjadsklfjalsdkjflad skfjla","sendDate":"2008/6/28 14:48:48","readDate":"2008/6/28 14:48:48","msgType ":0,"msgState":0,"isFile":""},{"id":21,"fromId":4,"toId":3,"title":"测试","msgTe xt":"jdskfjadsklfjadlskfjkladsfjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkf jsadlkfjasldkfjadsklfjladskfjadslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkf jlakdsjflakdsfjakdsfjladsfkjasdlfjadsklfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:48","readDate":"2008/6/28 14:48:48","msgType":0,"msgState":0,"isFile":""} ,{"id":22,"fromId":4,"toId":3,"title":"测试","msgText":"jdskfjadsklfjadlskfjklad sfjlkadsjflkdsjfklsdjklfjdslfkjadslkfjasdlfkjasldkfjsadlkfjasldkfjadsklfjladskfj adslkfjkladsfjlaskdfjlkadsjflkadsjfkljadslkfjadslkfjlakdsjflakdsfjakdsfjladsfkja sdlfjadsklfjalsdkjfladskfjla","sendDate":"2008/6/28 14:48:48","readDate":"2008/6 /28 14:48:48","msgType":0,"msgState":0,"isFile":""}]} |