【问题标题】:How to covert a DataTable to Json Format? [duplicate]如何将 DataTable 转换为 Json 格式? [复制]
【发布时间】:2016-10-03 07:26:50
【问题描述】:

我正在尝试将从数据库中获取的 DataTable 转换为 Json 格式。但我收到一个错误。

public string ConvertTableToJSON(DataTable objDataTable)
    {
        ArrayList columnNames = new ArrayList();
        int rowCount = objDataTable.Rows.Count;
        int currentRow = 1;

        string json = "";

        //fetching column names
        foreach (DataColumn objColumn in objDataTable.Columns)
        {
            columnNames.Add(objColumn.ColumnName);
        }

        //generating json string for each row
        foreach (DataRow objRow in objDataTable.Rows)
        {
            json = json + "{";
            json = json + ConvertRowToJSON(objRow, columnNames);
            json = json + "}";

            if (currentRow != rowCount)
            {
                json = json + ",";
            }

            currentRow = currentRow + 1;
        }

        return json;
    }

以上是DataTable转Json格式的代码

" Index was outside the bounds of the array." 是调试代码时的错误。这个错误发生在行

if (data[0] == '[' || data[0] == '{')

【问题讨论】:

    标签: c# json


    【解决方案1】:

    该方法用于将datatable转成json字符串

    public string ConvertDataTabletoJSON(DataTable dt)
    {
    
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    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);
    }
    

    它使用System.Web.Script.Serialization.JavaScriptSerializer将内容序列化为JSON格式:

    【讨论】:

      【解决方案2】:

      您可以通过以下代码使用 JavaScriptSerializer 将 DataTable 转换为 JSON

      public string DataTableToJsonWithJavaScriptSerializer(DataTable objDataTable)
      {
       JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
       List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
       Dictionary<string, object> childRow;
       foreach (DataRow row in objDataTable.Rows)
       {
       childRow = new Dictionary<string, object>();
       foreach (DataColumn col in table.Columns)
       {
       childRow.Add(col.ColumnName, row[col]);
       }
       parentRow.Add(childRow);
       }
       return jsSerializer.Serialize(parentRow);
      }
      

      您可以使用 Json.Net DLL 并将数据表转换为 json,例如

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

      包含库。

      Newtonsoft.Json;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-16
        • 2020-08-30
        • 1970-01-01
        • 2013-06-14
        • 2020-04-13
        • 1970-01-01
        • 2021-11-06
        • 2019-11-12
        相关资源
        最近更新 更多