• jquery datagrid 后台获取datatable处理成正确的json字符串


    1.处理点所有的换行

    /// <summary> 

    /// 去掉换行符        

    /// </summary>       

    /// <param name="str"></param>    

    /// <returns></returns>

     public string GetDeleteBR(string   strinput){
     string p = "\\n|\r\n"; //数据库的的换行是\n

     string returnstr = System.Text.RegularExpressions.Regex.Replace(strinput, p, " ");

    return returnstr;

    }

    2.转换成对应的json数据格式的字符串

    /// <summary>

    /// datatable to json

    /// </summary>

    /// <param name="count"></param>

    /// <param name="page"></param> 

    /// <param name="dt"></param>

    /// <returns></returns>

     public string DataTableToJson(int count, int page, DataTable dt){

      StringBuilder Json = new StringBuilder();  

      Json.Append("{");            

      Json.Append("\"total\":" + count + ",");            

      Json.Append("\"rows\":[");

       if (dt != null){

            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() +

                             "\":\""           + GetDeleteBR (dt.Rows[i][j].ToString())+ "\"");    

                             if (j < dt.Columns.Count - 1)

                                {

                                     Json.Append(",");

                                    Json.Append("\r\n");

                                 }    

                           }                   

                           Json.Append("}");         

                       if (i < dt.Rows.Count - 1)

                        {

                         Json.Append(","); 

                        }    

                     }       

              }         

        }            

            Json.Append("],");     

            Json.Append("\"index\":" + page);        

           Json.Append("}");

           return Json.ToString();

    }

                return Json.ToString();

  • 相关阅读:
    hdu4622(后缀自动机模板)
    51nod1445(最短路)
    Java原子类--框架
    Java锁--Semaphore
    Java锁--CyclicBarrier
    Java锁--CountDownLatch
    Java锁--共享锁和ReentrantReadWriteLock
    Java锁--LockSupport
    Java锁--Condition
    Java锁--非公平锁
  • 原文地址:https://www.cnblogs.com/linsu/p/2314259.html
Copyright © 2020-2023  润新知