• JSON与DataTable(DataSet)相互转化


      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(""Head":[ ");
                    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;
                }
            }

  • 相关阅读:
    c# 序列化和反序列化
    dataGridView通过checkbox选择数据
    获取文件后缀名(迟点写入自己的类库里面去) .NET
    刚在《微软中国msdn围脖》看到面试题
    jquery 遍历下拉框
    html代码转换成为纯文本
    获取ListBox中多选值
    递归算法运用到实际项目,可行吗?
    ExtJs学习笔记1
    JS 可变参数 arguments
  • 原文地址:https://www.cnblogs.com/-maomao/p/4075961.html
Copyright © 2020-2023  润新知