C#实现DataTable,List,Json和对象集合以及json序列化转换的方法

C#实现DataTable,List,Json和对象集合以及json序列化转换的方法

DataTable转字符串

  public static string DataTableToJsonWithJavaScriptSerializer(DataTable table)
        {
            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
            Dictionary<string, object> childRow;
            foreach (DataRow row in table.Rows)
            {
                childRow = new Dictionary<string, object>();
                foreach (DataColumn col in table.Columns)
                {
                    childRow.Add(col.ColumnName, row[col]);
                }
                parentRow.Add(childRow);
            }
            return jsSerializer.Serialize(parentRow);
        }

DataTable转json 时间格式化

public static string DataTableToJson(DataTable dt)
        {
            if (dt == null && dt.Rows.Count <= 0)
            {
                return "";
            }
            else
            {
                IsoDateTimeConverter convert = new IsoDateTimeConverter();
                convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                return JsonConvert.SerializeObject(dt, Formatting.None, convert);

            }

        }

List转换成DataTable

 public static DataTable ToDataTable<T>(this IList<T> data)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
            DataTable dt = new DataTable();
            for (int i = 0; i < properties.Count; i++)
            {
                PropertyDescriptor property = properties[i];
                dt.Columns.Add(property.Name, property.PropertyType);
            }
            object[] values = new object[properties.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = properties[i].GetValue(item);
                }
                dt.Rows.Add(values);
            }
            return dt;
        }

List转成json

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();
        }
    }

对象集合转json

public static string ToJson(IEnumerable array)
{
  string jsonString = "[";
  foreach (object item in array)
  {
    jsonString += Json.ToJson(item) + ",";
  }
  return Json.DeleteLast(jsonString) + "]";
}

dataset转换成json

public static string ToJson(DataSet dataSet)
{
  string jsonString = "{";
  foreach (DataTable table in dataSet.Tables)
  {
    jsonString += "\"" + ToJson(table.TableName) + "\":" + ToJson(table) + ",";
  }
  return jsonString = DeleteLast(jsonString) + "}";
  }

json反序列化

  public static T GetObjFromStr(string t)
        {
            return JsonConvert.DeserializeObject<T>(t);
        }

json序列化

 public static string GetStrFromObj(T t)
        {
            return JsonConvert.SerializeObject(t);
        }
**object全部字段json化**
public static string ToJSON(object obj)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    return serializer.Serialize(obj);
}

object全部字段json化,带count
public static string ToJSON(object obj, int rowCount)
{
    string strJson = ToJSON(obj);
    string strTemp = "totalCount:{0},data:[{1}]";
    return "{" + String.Format(strTemp, rowCount, strJson) + "}";
}

List全部字段json化
public static string ToJSON(IList list, int rowCount)
{
    string strJson = ToJSON(list);
    string strTemp = "totalCount:{0},data:{1}";
    return "{" + String.Format(strTemp, rowCount, strJson) + "}";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值