C# Json 格式转换

        public static string ConvertToJson(System.Data.DataTable dt)
        {

            System.Text.StringBuilder retVal = new System.Text.StringBuilder();
            retVal.Append("[");
            int RowCount = 0, ColumnCount = 0;
            foreach (DataRow row in dt.Rows)
            {
                RowCount++;
                retVal.Append("{");
                foreach (DataColumn column in dt.Columns)
                {
                    ColumnCount++;
                    retVal.AppendFormat("'{0}':'{1}'{2}", column.ColumnName, (row[column].ToString()), ColumnCount == dt.Columns.Count ? "" : ",");
                }
                ColumnCount = 0;
                retVal.Append("}");
                retVal.AppendFormat("{0}", RowCount == dt.Rows.Count ? "" : ",");
            }
            retVal.Append("]");
            return retVal.ToString();
        }

阅读更多
换一批

没有更多推荐了,返回首页