From DataTable in C# .NET to JSON

前端 未结 7 665
一整个雨季
一整个雨季 2020-12-04 18:12

I am pretty new at C# and .NET, but I\'ve made this code to call a stored procedure, and I then want to take the returned DataTable and convert it to JSON.

         


        
7条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 18:54

    I use JavaScriptSerializer + LINQ

    return new JavaScriptSerializer().Serialize(
      dataTable.Rows.Cast()
      .Select(row => row.Table.Columns.Cast()
        .ToDictionary(col => col.ColumnName, col => row[col.ColumnName]))
      .ToList()
    );
    

提交回复
热议问题