Created
January 24, 2019 20:41
-
-
Save jmbeach/ec94e1a6048bd1bb4a2c737ad2d5f539 to your computer and use it in GitHub Desktop.
Newtonsoft.Json Serialize DataTable Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var dt = new DataTable(); | |
dt.Columns.Add(new DataColumn("ID", typeof(int))); | |
dt.Columns.Add(new DataColumn("Name", typeof(string))); | |
var row = dt.NewRow(); | |
row["ID"] = 1; | |
row["Name"] = "John Snow"; | |
var row2 = dt.NewRow(); | |
row2["ID"] = 2; | |
row2["Name"] = "Iron Man"; | |
dt.Rows.Add(row); | |
dt.Rows.Add(row2); | |
JsonConvert.SerializeObject(dt).Dump(); | |
// prints: [{"ID":1,"Name":"John Snow"},{"ID":2,"Name":"Iron Man"}] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment