html 表格转json数据格式,TabletoJson,表格转json数据格式

//引用方法

DataSet dataSetnum = fy.GetPageDataset(table, "*", "PropertyID", 1, 1, 1, 1, "");

DataSet dataSet = fy.GetPageDataset(table, "*", "PropertyID",  Convert.ToInt32(Request["limit"]), Convert.ToInt32(Request["page"]), 0, 0, "");

//调用上面的将DataSet转换为List

department menus = TabletoJson.DataSetToEntity(dataSet, 0);

Response.Write(JsonConvert.SerializeObject(menus)+"
");

List tt = ToListByReflect(dataSetc.Tables[0]);

Response.Write(JsonConvert.SerializeObject(tt)+"
");

public class department

{

public int id { get; set; }

public int dictionary_id { get; set; }

public string zg_name { get; set; }

public int two_id { get; set; }

public string name { get; set; }

public string tel { get; set; }

public string add_txt { get; set; }

public string mark { get; set; }

public string EmpID { get; set; }

public int working { get; set; }

public string addtime { get; set; }

}

public static List ToListByReflect(DataTable dt) where T : new()

{

List ts = new List();

string tempName = string.Empty;

foreach (DataRow dr in dt.Rows)

{

T t = new T();

PropertyInfo[] propertys = t.GetType().GetProperties();

foreach (PropertyInfo pi in propertys)

{

tempName = pi.Name;

if (dt.Columns.Contains(tempName))

{

object value = dr[tempName];

if (value != DBNull.Value)

{

Type Typeof = pi.PropertyType;      //这里是获取实体类字段属性

if (Typeof.IsGenericType && Typeof.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))

{

Typeof = Nullable.GetUnderlyingType(Typeof);

}

pi.SetValue(t, Convert.ChangeType(value, Typeof), null);

// pi.SetValue(t, value, null);

}

}

}

ts.Add(t);

}

return ts;

}

///

/// DataSet转换为实体类

///

/// 实体类

/// DataSet

/// 待转换数据表索引

/// 实体类

public static T DataSetToEntity(DataSet p_DataSet, int p_TableIndex)

{

if (p_DataSet == null || p_DataSet.Tables.Count < 0)

return default(T);

if (p_TableIndex > p_DataSet.Tables.Count - 1)

return default(T);

if (p_TableIndex < 0)

p_TableIndex = 0;

if (p_DataSet.Tables[p_TableIndex].Rows.Count <= 0)

return default(T);

DataRow p_Data = p_DataSet.Tables[p_TableIndex].Rows[0];

// 返回值初始化

T _t = (T)Activator.CreateInstance(typeof(T));

PropertyInfo[] propertys = _t.GetType().GetProperties();

foreach (PropertyInfo pi in propertys)

{

if (p_DataSet.Tables[p_TableIndex].Columns.IndexOf(pi.Name.ToUpper()) != -1 && p_Data[pi.Name.ToUpper()] != DBNull.Value)

{

Type Typeof = pi.PropertyType;      //这里是获取实体类字段属性

if (Typeof.IsGenericType && Typeof.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))

{

Typeof = Nullable.GetUnderlyingType(Typeof);

}

pi.SetValue(_t, Convert.ChangeType(p_Data[pi.Name.ToUpper()], Typeof), null);

}

else

{

pi.SetValue(_t, null, null);

}

}

return _t;

}

///

/// DataSet转换为实体列表

///

/// 实体类

/// DataSet

/// 待转换数据表索引

/// 实体类列表

public static IList DataSetToEntityList(DataSet p_DataSet, int p_TableIndex)

{

if (p_DataSet == null || p_DataSet.Tables.Count < 0)

return default(IList);

if (p_TableIndex > p_DataSet.Tables.Count - 1)

return default(IList);

if (p_TableIndex < 0)

p_TableIndex = 0;

if (p_DataSet.Tables[p_TableIndex].Rows.Count <= 0)

return default(IList);

DataTable p_Data = p_DataSet.Tables[p_TableIndex];

// 返回值初始化

IList result = new List();

for (int j = 0; j < p_Data.Rows.Count; j++)

{

T _t = (T)Activator.CreateInstance(typeof(T));

PropertyInfo[] propertys = _t.GetType().GetProperties();

foreach (PropertyInfo pi in propertys)

{

if (p_Data.Columns.IndexOf(pi.Name.ToUpper()) != -1 && p_Data.Rows[j][pi.Name.ToUpper()] != DBNull.Value)

{

Type Typeof = pi.PropertyType;      //这里是获取实体类字段属性

if (Typeof.IsGenericType && Typeof.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))

{

Typeof = Nullable.GetUnderlyingType(Typeof);

}

pi.SetValue(_t, Convert.ChangeType(p_Data.Rows[j][pi.Name.ToUpper()], Typeof), null);

}

else

{

pi.SetValue(_t, null, null);

}

}

result.Add(_t);

}

return result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值