Sanjay gautam

Sanjay gautam

  • 1.8k
  • 63
  • 913

convert table to json

Oct 18 2019 12:49 AM
  1. static public string table2Json(DataSet ds, int table_no)  
  2.        {  
  3.            try  
  4.            {  
  5.                // Given a Dataset & Table NO , function returns a 2 dim JSON Array of values in that table                          
  6.                int rcnt = ds.Tables[table_no].Rows.Count; // Row Count  
  7.                int ccnt = ds.Tables[table_no].Columns.Count; // Col Count  
  8.   
  9.   
  10.                object[][] tb = new object[rcnt][];  
  11.   
  12.                int r = 0;  
  13.                foreach (DataRow dr in ds.Tables[table_no].Rows)  
  14.                {  
  15.                    tb[r] = new object[ccnt];  
  16.                    for (int col = 0; col < ccnt; col++)  
  17.                    {  
  18.                        tb[r][col] = dr[col];  
  19.   
  20.                        if ((tb[r][col]).Equals(System.DBNull.Value))  
  21.                            tb[r][col] = "";  
  22.                    }  
  23.                    r++;  
  24.                }  
  25.                 
  26.                var table= JsonConvert.SerializeObject(tb);  
  27.                return table;  
  28.            }  
I have some code for convert table to json string i want to remove  for loo from these code how does it is possible

Answers (3)