json。net 序列化 反序列化 转成datatable
public string GetRepairInfolist(int paper,string swhere)
{
int startIndex = (paper - 1) * 8 + 1;
int endIndex = paper * 8;
string jsonData = string.Empty;
if(string.IsNullOrEmpty(swhere))
{
swhere = "";
}
//if (!string.IsNullOrEmpty(swhere))
//{
DataSet dt = bll.GetListByPage(swhere, "", startIndex, endIndex);
List<Jasonzhao.Model.Repairmodel> lstModel = new List<Jasonzhao.Model.Repairmodel>();
for (int d = 0; d < dt.Tables[0].Rows.Count; d++)
{
Jasonzhao.Model.Repairmodel model = new Jasonzhao.Model.Repairmodel();
model.Repairid = int.Parse(dt.Tables[0].Rows[d]["id"].ToString());
model.RepairNum = dt.Tables[0].Rows[d]["RepairNum"].ToString();
model.RepairHuman = dt.Tables[0].Rows[d]["RepairHuman"].ToString();
model.RepairHumanPhone = dt.Tables[0].Rows[d]["RepairHumanPhone"].ToString();
model.RepairCatalogId = dt.Tables[0].Rows[d]["RepairCatalogId"].ToString();
model.Position = dt.Tables[0].Rows[d]["Position"].ToString();
model.DescN = dt.Tables[0].Rows[d]["DescN"].ToString();
model.ImgUrl = dt.Tables[0].Rows[d]["ImgUrl"].ToString();
model.RepairStatus = dt.Tables[0].Rows[d]["RepairStatus"].ToString();
model.RepairTime = dt.Tables[0].Rows[d]["RepairTime"].ToString();
model.RepairHumanText = dt.Tables[0].Rows[d]["RepairHumanText"].ToString();
lstModel.Add(model);
}
//Json.NET序列化
jsonData = Jasonzhao.Common.JsonHelper.json(lstModel);
// }
return jsonData;
}
//后台反序列化
public static DataTable JsonToDataSet(string Jsonstring)
{
DataTable ds = new DataTable();
try
{
Jsonstring = Jsonstring.Replace("\\", "").Trim();
Jsonstring = Jsonstring.Substring(1, Jsonstring.Length - 2);
List<Jasonzhao.Model.Repairmodel> lstModel = new List<Jasonzhao.Model.Repairmodel>();
lstModel = (List<Jasonzhao.Model.Repairmodel>)Jasonzhao.Common.JsonHelper.jsonDes<List<Jasonzhao.Model.Repairmodel>>(Jsonstring);
ds = ListToTable<Jasonzhao.Model.Repairmodel>(lstModel);
}
catch { }
return ds;
}
//转成datatable
public static DataTable ListToTable<T>(List<T> list)
{
Type tp = typeof(T);
PropertyInfo[] proInfos = tp.GetProperties();
DataTable dt = new DataTable();
foreach (var item in proInfos)
{
dt.Columns.Add(item.Name, item.PropertyType); //添加列明及对应类型
}
foreach (var item in list)
{
DataRow dr = dt.NewRow();
foreach (var proInfo in proInfos)
{
object obj = proInfo.GetValue(item);
if (obj == null)
{
continue;
}
dr[proInfo.Name] = obj;
}
dt.Rows.Add(dr);
}
return dt;
}