将DataTable转换为JSON

时间:2016-11-18 11:25:57

标签: c# .net json datatable

我有以下代码。

public class Game
{
    public string gamenumber { get; set; }
    public string league { get; set; }
    public string date { get; set; }
}

public class getGames
{
    public List<Game> games { get; set; }
}

dt = mySQL.getAsDataTable("SELECT gamenumber, date, league FROM vSpiele WHERE gamenumber LIKE '" + sgamenumber + "'", null);

var getGames = new getGames
{
    games = new List<Game>
    {
        new Game
        {
            gamenumber = dt.Rows[0]["gamenumber"].ToString(),
            league = dt.Rows[0]["league"].ToString(),
            date= dt.Rows[0]["date"].ToString(),
        }
    }
 };
 string json = JsonConvert.SerializeObject(getSpiele);

我的输出显示:

{
    "Games": [{
        "gamenumber": "123456",
        "league": "Test League",
        "date": "03.09.2016 15:00:00",
    }]
}

我的问题是我不仅需要第0行,还需要表格的所有行。我想用for-loop实际解决这个问题,但我总是遇到错误。有没有人知道我如何得到表格的剩余数据?

4 个答案:

答案 0 :(得分:0)

您需要遍历数据表的行,因为不需要索引,您可以使用foreach而不是for-loop

dt = mySQL.getAsDataTable("SELECT gamenumber, date, league FROM vSpiele 
                           WHERE gamenumber LIKE '" + sgamenumber + "'", null);

var getGames = new getGames(); 
getGames.games = new List<Game>();
foreach(DataRow row in dt.Rows)
{
   getGames.games.Add(new Game(){
      gamenumber = row["gamenumber"].ToString(),
      league = row["league"].ToString(),
      date= row["date"].ToString()
   });
}
string json = JsonConvert.SerializeObject(getSpiele);

如果您愿意,也可以使用LINQ

var games = (from DataRow row in dt.Rows
select new Game() {
    gamenumber = row["gamenumber"].ToString(), 
    league = row["league"].ToString(), 
    date = row["date"].ToString()
}).ToList();

答案 1 :(得分:0)

假设dtDataTable,您可以执行foreach循环:

var games = new List<Game>();

foreach(DataRow row in dt.Rows)
{
   games.add(new Game() 
   {
     gamenumber = row["gamenumber"].ToString(),
     league = row["league"].ToString(),
     date= row["date"].ToString()
   });
}

答案 2 :(得分:0)

您应该能够循环遍历数据表中的所有行,如下所示:

dt = mySQL.getAsDataTable("SELECT gamenumber, date, league FROM vSpiele WHERE gamenumber LIKE '" + sgamenumber + "'", null);
var allGames = new getGames { games = new List<Game>() };

foreach(DataRow dr in dt.Rows)
{                
    allGames.games.Add(new Game
    {
        gamenumber = dr["gamenumber"].ToString(),
        league = dr["league"].ToString(),
        date= dr["date"].ToString(),
    });
}


string json = JsonConvert.SerializeObject(allGames);

答案 3 :(得分:0)

使用Javascript序列化程序可以轻松完成此操作:

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

如果您需要更多详细信息,请点击此处http://www.aspdotnet-suresh.com/2013/05/c-convert-datatable-to-json-string-in-c.html