我有以下代碼。將DataTable轉換爲JSON
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循環來解決問題,但我總是遇到一個錯誤。有沒有人有一個想法,我怎麼來到桌子的剩餘數據?
可能的重複[在C#中將數據錶轉換爲JSON](https://stackoverflow.com/questions/17398019/convert-datatable-to-json-in -c-sharp) –