JS轉爲json數據、.NET DataTable轉化爲json格式

series: [dataMed = eval("(" + dataMed + ")")]     //轉換爲json格式 string 


public static string DataSetToJson(DataTable dt)
  
{
 
       string json = string.Empty;
 
       try
 
       {
 
           if (dt==null||dt.Rows.Count == 0)
 
           {
 
               return "";
 
           }
 
           json = "{";
 
           json += "'table" + 1 + "':[";
 
           for (int i = 0; i < dt.Rows.Count; i++)
 
           {
 
               json += "{";
 
               for (int j = 0; j < dt.Columns.Count; j++)
 
               {
 
                   json += "'" + dt.Columns[j].ColumnName + "':'" + dt.Rows[i][j].ToString() + "'";
 
                   if (j != dt.Columns.Count - 1)
 
                   {
 
                       json += ",";
 
                   }
 
               }
 
               json += "}";
 
               if (i != dt.Rows.Count - 1)
 
               {
 
                   json += ",";
 
               }
 
           }
 
           json += "]";
 
           json += "}";
 
       }
 
       catch (Exception ex)
 
       {






           throw new Exception(ex.Message);
 
       }
 
       return json;
 
   }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章