C# ToJson 工具类编写

2 篇文章 0 订阅

 有段时间在使用Ajax处理Json问题

虽然现在.net 3.0以后有了关于处理Json的类库

 

但是现在很多服务器还不支持

 

没办法有只有动手开始自己写了

 

 

  1. using System;
  2. using System.Reflection;
  3. using System.Collections;
  4. using System.Data;
  5. using System.Data.Common;
  6. /// <summary>
  7. /// Json转换工具
  8. /// 作者:吴奇凡
  9. /// 用于转换基本的对象
  10. /// </summary>
  11. public class FxJson
  12. {
  13.     private FxJson()
  14.     {
  15.     }
  16.     /// <summary>
  17.     /// 对象转换为Json字符串
  18.     /// </summary>
  19.     /// <param name="jsonObject">对象</param>
  20.     /// <returns>Json字符串</returns>
  21.     public static string ToJsonAll(object jsonObject)
  22.     {
  23.         string jsonString = "{";
  24.         PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties();
  25.         for (int i = 0; i < propertyInfo.Length; i++)
  26.         {
  27.             object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
  28.             string value = string.Empty;
  29.             if (objectValue is DateTime || objectValue is string || objectValue is Guid || objectValue is TimeSpan)
  30.             {
  31.                 value = "'" + objectValue + "'";
  32.             }
  33.             else if (objectValue is IEnumerable)
  34.             {
  35.                 value = ToString((IEnumerable)objectValue);
  36.             }
  37.             else
  38.             {
  39.                 value = objectValue.ToString();
  40.             }
  41.             jsonString += "/"" + propertyInfo[i].Name + "/":" + value + ",";
  42.         }
  43.         return FxJson.DeleteLast(jsonString) + "}";
  44.     }
  45.     /// <summary>
  46.     /// 对象集合转换Json
  47.     /// </summary>
  48.     /// <param name="array">集合对象</param>
  49.     /// <returns>Json字符串</returns>
  50.     public static string ToString(IEnumerable array)
  51.     {
  52.         string jsonString = "[";
  53.         foreach (object item in array)
  54.         {
  55.             jsonString += FxJson.ToJsonAll(item) + ",";
  56.         }
  57.         return FxJson.DeleteLast(jsonString) + "]";
  58.     }
  59.     /// <summary>
  60.     /// 普通集合转换Json
  61.     /// </summary>
  62.     /// <param name="array">集合对象</param>
  63.     /// <returns>Json字符串</returns>
  64.     public static string ToArrayString(IEnumerable array)
  65.     {
  66.         string jsonString = "[";
  67.         foreach (object item in array)
  68.         {
  69.             jsonString = item+",";   
  70.         }
  71.         return FxJson.DeleteLast(jsonString) + "]";
  72.     }
  73.     /// <summary>
  74.     /// 删除结尾字符
  75.     /// </summary>
  76.     /// <param name="str">需要删除的字符</param>
  77.     /// <returns>完成后的字符串</returns>
  78.     private static string DeleteLast(string str)
  79.     {
  80.         if (str.Length > 1)
  81.         {
  82.             return str.Substring(0, str.Length - 1);
  83.         }
  84.         return str;
  85.     }
  86.     /// <summary>
  87.     /// Datatable转换为Json
  88.     /// </summary>
  89.     /// <param name="table">Datatable对象</param>
  90.     /// <returns>Json字符串</returns>
  91.     public static string ToString(DataTable table)
  92.     {
  93.         string jsonString = "[";
  94.         DataRowCollection drc = table.Rows;
  95.         for (int i = 0; i < drc.Count; i++)
  96.         {
  97.             jsonString += "{";
  98.              foreach (DataColumn column in table.Columns)
  99.              {
  100.                  jsonString += "/"" + column.ColumnName + "/":";
  101.                  if (column.DataType == typeof(DateTime) || column.DataType== typeofstring))
  102.                  {
  103.                      jsonString += "/"" + drc[i][column.ColumnName] + "/",";
  104.                  }
  105.                  else
  106.                  {
  107.                      jsonString +=  drc[i][column.ColumnName] + ",";
  108.                  }
  109.              }
  110.              jsonString = DeleteLast(jsonString) + "},";
  111.         }
  112.         return DeleteLast(jsonString) + "]";
  113.     }
  114.     /// <summary>
  115.     /// DataReader转换为Json
  116.     /// </summary>
  117.     /// <param name="dataReader">DataReader对象</param>
  118.     /// <returns>Json字符串</returns>
  119.     public static string ToString(DbDataReader dataReader)
  120.     {
  121.         string jsonString = "[";
  122.         while (dataReader.Read())
  123.         {
  124.             jsonString += "{";
  125.             for (int i = 0; i < dataReader.FieldCount; i++)
  126.             {
  127.                 jsonString += "/"" + dataReader.GetName(i) + "/":";
  128.                 if (dataReader.GetFieldType(i) == typeof(DateTime) || dataReader.GetFieldType(i) == typeof(string))
  129.                 {
  130.                     jsonString += "/"" + dataReader[i] + "/",";
  131.                 }
  132.                 else
  133.                 {
  134.                     jsonString += dataReader[i] + ",";
  135.                 }
  136.             }
  137.             jsonString = DeleteLast(jsonString) + "}";
  138.         }
  139.         dataReader.Close();
  140.         return DeleteLast(jsonString) + "]";
  141.     }
  142.     /// <summary>
  143.     /// DataSet转换为Json
  144.     /// </summary>
  145.     /// <param name="dataSet">DataSet对象</param>
  146.     /// <returns>Json字符串</returns>
  147.     public static string ToString(DataSet dataSet)
  148.     {
  149.         string jsonString = "{";
  150.         foreach (DataTable table in dataSet.Tables)
  151.         {
  152.             jsonString += "/"" + table.TableName + "/":" + ToString(table) + ",";
  153.         }
  154.         return jsonString = DeleteLast(jsonString) + "}";
  155.     }
  156. }

 

 

 

 

嘿嘿试试感觉不错哦

 

 以下是测试记录

 

{"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":""}]}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值