文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Json转换工具

Json转换工具

时间:2011-03-14  来源:钟凯

 

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) + "}";  

     }  

 }  

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载