locked
deta set to json RRS feed

  • Question

  • User1740134225 posted

    i have some code i want to remove for loop how does it done by me please suggest

       static public string table2Json(DataSet ds, int table_no)
            {
                try
                {
                    // Given a Dataset & Table NO , function returns a 2 dim JSON Array of values in that table                        
                    int rcnt = ds.Tables[table_no].Rows.Count; // Row Count
                    int ccnt = ds.Tables[table_no].Columns.Count; // Col Count
    
    
                    object[][] tb = new object[rcnt][];
    
                    int r = 0;
                    foreach (DataRow dr in ds.Tables[table_no].Rows)
                    {
                        tb[r] = new object[ccnt];
                        for (int col = 0; col < ccnt; col++)
                        {
                            tb[r][col] = dr[col];
    
                            if ((tb[r][col]).Equals(System.DBNull.Value))
                                tb[r][col] = "";
                        }
                        r++;
                    }
                   
                    var table = JsonConvert.SerializeObject(tb);
                    return table;
                }
                catch (Exception ex)
                {
                    tools.log(ex.Message);
                    throw ex;
                }
            }

    Friday, October 18, 2019 8:12 AM

All replies

  • User665608656 posted

    Hi sanjay,

    According to your code, do you want to return the contents of datatable in the form of json?

    What is the specific purpose of the deletion loop you mentioned? Do you want to implement datatable to return in json format without using the loop?

    If you know the field name in the datatable , you can create a class to convert the datatable to the format of List<object> and then convert the format to json format, but the loop is still needed.

    You can refer to this link : https://stackoverflow.com/a/12834413

    In MVC, I suggest that you can create the entityframework to automatically generate the class of datatable, then use linq to directly obtain the datatable in the form of collection, and finally use jsonconvert.serializeobject to convert it to json format for return.

    How to create entityframework in mvc , you could refer to this link : Tutorial: Get Started with Entity Framework 6 Code First using MVC 5

    How to use entityframework with linq , you could refer to it: https://stackoverflow.com/a/658056

    Best Regards,

    YongQing.

    Friday, October 18, 2019 9:57 AM
  • User1740134225 posted

    I want to remove for loop because it take more time for exicution  an i did not remove loop just remove only for loop

    Friday, October 18, 2019 10:05 AM
  • User-474980206 posted

    there is no value in using a data table for your use, you should use a data reader and create the array collection directly. You could gain more perform by using a json data writer so you are not creating objects, just to loop thru to create json.

    Friday, October 18, 2019 8:19 PM
  • User665608656 posted

    Hi sanjay,

    I want to remove for loop because it take more time for exicution  an i did not remove loop just remove only for loop

    You can try to use Json.net Newtonsoft dll , it's very easy to get JSON data.

      public string DataTableToJsonWithJsonNet(DataTable table)
            {
                string jsonString = string.Empty;
                jsonString = JsonConvert.SerializeObject(table);
                return jsonString;
            }

    Best Regards,

    YongQing.

    Monday, October 21, 2019 1:36 AM