将DataTable或Ilist<>转换成JSON格式

using  System;
using  System.Data;
using  System.Text;
using  System.Collections.Generic;
using  System.Reflection;

///   <summary>
///  将DataTable或Ilist <> 转换成JSON格式
///   </summary>
public   class  ToJson
{
    
public  ToJson()
    {

    }
    
public   static   string  DataTableToJson( string  jsonName, DataTable dt)
    {
        StringBuilder Json 
=   new  StringBuilder();
        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 ++ )
                {
                    Json.Append(
" \ ""  + dt.Columns[j].ColumnName.ToString() +  " \ " :\ ""  + dt.Rows[i][j].ToString() +  " \ "" );
                    
if  (j  <  dt.Columns.Count  -   1 )
                    {
                        Json.Append(
" , " );
                    }
                }
                Json.Append(
" } " );
                
if  (i  <  dt.Rows.Count  -   1 )
                {
                    Json.Append(
" , " );
                }
            }
        }
        Json.Append(
" ]} " );
        
return  Json.ToString();
    }
    
public   static   string  ObjectToJson < T > ( string  jsonName, IList < T >  IL)
    {
        StringBuilder Json 
=   new  StringBuilder();
        Json.Append(
" {\ ""  + jsonName +  " \ " :[ " );
        
if  (IL.Count  >   0 )
        {
            
for  ( int  i  =   0 ; i  <  IL.Count; i ++ )
            {
                T obj 
=  Activator.CreateInstance < T > ();
                Type type 
=  obj.GetType();
                PropertyInfo[] pis 
=  type.GetProperties();
                Json.Append(
" { " );
                
for  ( int  j  =   0 ; j  <  pis.Length; j ++ )
                {
                    Json.Append(
" \ ""  + pis[j].Name.ToString() +  " \ " :\ ""  + pis[j].GetValue(IL[i], null) +  " \ "" );
                    
if  (j  <  pis.Length  -   1 )
                    {
                        Json.Append(
" , " );
                    }
                }
                Json.Append(
" } " );
                
if  (i  <  IL.Count  -   1 )
                {
                    Json.Append(
" , " );
                }
            }
        }
        Json.Append(
" ]} " );
        
return  Json.ToString();
    }
}


转载于:https://www.cnblogs.com/SealedLove/archive/2008/11/11/1331618.html

好的,我可以回答你的问题。利用反射可以实现 DataTableList<T> 之间的转换。 首先,我们需要定义一个通用的方法,该方法可以将 List<T> 转换为 DataTable,也可以将 DataTable 转换为 List<T>。该方法的实现需要借助反射机制。 下面是将 List<T> 转换为 DataTable 的代码: ```csharp public static DataTable ConvertToDataTable<T>(List<T> list) { DataTable table = new DataTable(); PropertyInfo[] properties = typeof(T).GetProperties(); foreach (PropertyInfo property in properties) { table.Columns.Add(property.Name, property.PropertyType); } foreach (T item in list) { DataRow row = table.NewRow(); foreach (PropertyInfo property in properties) { row[property.Name] = property.GetValue(item, null); } table.Rows.Add(row); } return table; } ``` 上面的代码中,我们首先使用反射机制获取类型 T 的所有属性信息,然后使用这些属性信息创建 DataTable 的列。接着,我们遍历 List<T> 中的每个元素,使用反射获取元素的属性值,并将这些属性值添加到 DataRow 中,最后将 DataRow 添加到 DataTable 中。 下面是将 DataTable 转换为 List<T> 的代码: ```csharp public static List<T> ConvertToList<T>(DataTable table) where T : new() { List<T> list = new List<T>(); PropertyInfo[] properties = typeof(T).GetProperties(); foreach (DataRow row in table.Rows) { T item = new T(); foreach (PropertyInfo property in properties) { if (table.Columns.Contains(property.Name)) { property.SetValue(item, row[property.Name], null); } } list.Add(item); } return list; } ``` 上面的代码中,我们同样使用反射机制获取类型 T 的所有属性信息,然后遍历 DataTable 中的每个 DataRow,使用反射设置每个 DataRow 中对应的属性值,并将 T 对象添加到 List<T> 中。 希望这些代码可以帮助你解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值