• Datatable转换为Json


    /// <summary>
    /// Datatable转换为Json
    /// </summary>
    /// <param name="table">Datatable对象</param>
    /// <returns>Json字符串</returns>
    public static string ToJson(this DataTable dt)
    {
    StringBuilder jsonString = new StringBuilder();
    jsonString.Append("[");
    DataRowCollection drc = dt.Rows;
    for (int i = 0; i < drc.Count; i++)
    {
    jsonString.Append("{");
    for (int j = 0; j < dt.Columns.Count; j++)
    {
    string strKey = dt.Columns[j].ColumnName;
    string strValue = drc[i][j].ToString();
    Type type = dt.Columns[j].DataType;
    jsonString.Append(""" + strKey + "":");
    strValue = StringFormat(strValue, type);
    if (j < dt.Columns.Count - 1)
    {
    jsonString.Append(strValue + ",");
    }
    else
    {
    jsonString.Append(strValue);
    }
    }
    jsonString.Append("},");
    }
    jsonString.Remove(jsonString.Length - 1, 1);
    if (jsonString.Length != 0)
    {
    jsonString.Append("]");
    }
    return jsonString.ToString();
    }
  • 相关阅读:
    寒假自学1.11
    寒假学习1.10
    寒假自学1.09
    寒假自学1.08-python的安装学习
    寒假1.08
    寒假自学1.07
    寒假自学1.06-tensorflow安装过程
    java & | 运算符
    java << >>
    java基础百位数十位数个位数
  • 原文地址:https://www.cnblogs.com/ly-03-04/p/12171579.html
Copyright © 2020-2023  润新知