empty DataTable to Json with columns name

蓝咒 提交于 2019-12-20 03:29:11

问题


My app is divided in two parts : client and server. The server side ask a sql database and store the results in a DataTable. Then i use the next function to convert the DataTable to Json to send it to client.

The problem is when there are no results, i get an empty string. I would like to get a string which will allow the client side to display an empty datatable but with column names, i mean with column header !

Here is the function on the server side to convert datatable to Json:

 public string ConvertDataTabletoString(DataTable dt)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            serializer.MaxJsonLength = Int32.MaxValue;
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            return serializer.Serialize(rows);
        }

And here is the function on client side to convert Json to DataTable back:

return (DataTable)JsonConvert.DeserializeObject(clientSoap.dataCommand(command), (typeof(DataTable)));

Thanks in advance !!


回答1:


In the "no data rows" scenario, your code is serializing an empty dictionary. If you want to have your list of columns you could add a snippet of code like the following to ensure that your rows dictionary isn't empty.

    ...
    rows.Add(row);
}
if (rows.Count == 0)
{
    var row = new Dictionary<string, object>();
    foreach (DataColumn col in dt.Columns)
        row.Add(col.ColumnName, "");
    rows.Add(row);
}
return serializer.Serialize(rows);    

If the client is sending data back to the server you will need to do something to ensure that the server doesn't try to store the empty row as data.




回答2:


I used the response of @Richard and i created a function that check if we returned an empty datatable to delete the new empty row created. It's not 100% nice but its work fine.

On the server side :

public string ConvertDataTabletoString(DataTable dt)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            serializer.MaxJsonLength = Int32.MaxValue;
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            if (dt.Rows.Count == 0)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                    row.Add(col.ColumnName, "");
                rows.Add(row);
                return "empty" + serializer.Serialize(rows);
            }

            else
            {
                foreach (DataRow dr in dt.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
                return serializer.Serialize(rows);
            }
        }

On the client side:

DataTable JsonToDataTable (string text)
        {
            if(text.Substring(0,5) == "empty")
            {
                text = text.Remove(0, 5);
                DataTable dt = (DataTable)JsonConvert.DeserializeObject(text, (typeof(DataTable)));
                dt.Rows[0].Delete();
                return dt;
            }
            return (DataTable)JsonConvert.DeserializeObject(text, (typeof(DataTable)));
        }


来源:https://stackoverflow.com/questions/40110498/empty-datatable-to-json-with-columns-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!