• dataTable转换成Json格式


     #region dataTable转换成Json格式
            /// <summary>
            /// 
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string DataTableToJson(DataTable dt)
            {
                if (dt == null)
                    return string.Empty;
                StringBuilder Json = new StringBuilder();
                Json.Append("[");
                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();
            }
    
            #endregion

     或者

     private string GetJson(DataTable dt)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("[");
                foreach (DataRow row in dt.Rows)
                {
                    sb.Append("{");
                    foreach (DataColumn column in dt.Columns)
                    {
                        string value = row[column.ColumnName].ToString();
                        value = formatStr(value);
                        sb.Append(column.ColumnName + ":"" + value + "",");
                    }
                    if (sb.ToString().EndsWith(","))
                    {
                        sb.Remove(sb.Length - 1, 1);
                    }
                    sb.Append("},");
                }
                if (sb.ToString().EndsWith(","))
                {
                    sb.Remove(sb.Length - 1, 1);
                }
                sb.Append("]");
                return sb.ToString();
            }
    
            private string formatStr(string str)
            {
                if (str.Contains("""))
                {
                    str = str.Replace(""", "");
                }
                if (str.Contains("
    "))
                {
                    str = str.Replace("
    ", "");
                }
                return str;
            }
  • 相关阅读:
    codevs1076 排序
    codevs1075 明明的随机数
    codevs1205 单词翻转
    codevs1204 寻找子串位置
    codevs2235 机票打折
    codevs1206 保留两位小数
    codevs1203 判断浮点数是否相等
    codevs1202 求和
    codevs1201 最小数和最大数
    Static Sushi AtCoder
  • 原文地址:https://www.cnblogs.com/macT/p/10726645.html
Copyright © 2020-2023  润新知