2016-11-18 153 views
0

我有以下代碼。將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循環來解決問題,但我總是遇到一個錯誤。有沒有人有一個想法,我怎麼來到桌子的剩餘數據?

+0

可能的重複[在C#中將數據錶轉換爲JSON](https://stackoverflow.com/questions/17398019/convert-datatable-to-json-in -c-sharp) –

回答

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(); 
+0

謝謝:)你救了我一天 – Hadda

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() 
    }); 
} 
0

您應該能夠遍歷數據表中的所有行,如下所示:

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); 
} 

如果您需要:

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);