zoukankan      html  css  js  c++  java
  • DataTable转Json方法

    public static string CreateJsonParameters(DataTable dt)
            {
                /**//**/
                /**//* /****************************************************************************
              * Without goingin to the depth of the functioning of this Method, i will try to give an overview
              * As soon as this method gets a DataTable it starts to convert it into JSON String,
              * it takes each row and in each row it grabs the cell name and its data.
              * This kind of JSON is very usefull when developer have to have Column name of the .
              * Values Can be Access on clien in this way. OBJ.HEAD[0].<ColumnName>
              * NOTE: One negative point. by this method user will not be able to call any cell by its index.
             * *************************************************************************/
                StringBuilder JsonString = new StringBuilder();
                //Exception Handling        
                if (dt != null && dt.Rows.Count > 0)
                {
                    JsonString.Append("{ ");
                    JsonString.Append("\"T_blog\":[ ");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        JsonString.Append("{ ");
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            if (j < dt.Columns.Count - 1)
                            {
                                JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                            }
                            else if (j == dt.Columns.Count - 1)
                            {
                                JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                            }
                        }
                        /**//**/
                        /**//*end Of String*/
                        if (i == dt.Rows.Count - 1)
                        {
                            JsonString.Append("} ");
                        }
                        else
                        {
                            JsonString.Append("}, ");
                        }
                    }
                    JsonString.Append("]}");
                    return JsonString.ToString();
                }
                else
                {
                    return null;
                }
            }

    效果图如下:

    {"T_blog":
      [
       {"id":"14","title":"北京奥运开幕式","addtime":"2008-08-08"},
       {"id":"15","title":"网络文化","addtime":"2008-09-12"},
       {"id":"17","title":"北京下雨了","addtime":"2008-09-19"},
       {"id":"21","title":"深圳地铁通了","addtime":"2008-09-25"}
      ]
    }
    代码 
    
    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->    /// <summary>
        /// 将一个数据表转换成一个JSON字符串,在客户端可以直接转换成二维数组。
        /// </summary>
        /// <param name="source">需要转换的表。</param>
        /// <returns></returns>
        public static string DataTableToJson(DataTable source)
        {
            if (source.Rows.Count == 0)
                return "";
            StringBuilder sb = new StringBuilder("[");
            foreach (DataRow row in source.Rows)
            {
                sb.Append("[");
                for (int i = 0; i < source.Columns.Count; i++)
                {
                    sb.Append('"' + row[i].ToString() + "\",");
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append("],");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append("]");
            return sb.ToString();
        }
    
    
        /// <summary>
        /// 反回JSON数据到前台
        /// </summary>
        /// <param name="dt">数据表</param>
        /// <returns>JSON字符串</returns>
        public string CreateJsonParameters(DataTable dt)
        {
            StringBuilder JsonString = new StringBuilder();
            //Exception Handling        
            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("{ ");
                JsonString.Append("\"TableInfo\":[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                        }
                    }
                    /**/
                    /*end Of String*/
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("]}");
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }
  • 相关阅读:
    【知识强化】第四章 网络层 4.1 网络层的功能
    【知识强化】第三章 数据链路层 3.8 数据链路层设备
    【知识强化】第三章 数据链路层 3.7 广域网
    【知识强化】第三章 数据链路层 3.6 局域网
    【知识强化】第三章 数据链路层 3.5 介质访问控制
    【知识强化】第三章 数据链路层 3.4 流量控制与可靠传输机制
    ASP.NET MVC入门之再不学习就真的out了
    ASP.NET MVC系列:添加模型的验证规则
    ASP.NET MVC系列:为已有模型添加新的属性
    ASP.NET MVC系列:为视图添加查询功能
  • 原文地址:https://www.cnblogs.com/lovenan/p/2805533.html
Copyright © 2011-2022 走看看