using System; using System.Data; using Newtonsoft.Json; class Program { static void Main() { // 创建 DataTable DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Age", typeof(int)); table.Rows.Add("Alice", 25); table.Rows.Add("Bob", 30); // DataTable 转 JSON string json = JsonConvert.SerializeObject(table); Console.WriteLine(json); // JSON 转 DataTable string jsonData = "[{\"Name\":\"Charlie\",\"Age\":35},{\"Name\":\"David\",\"Age\":40}]"; DataTable deserializedTable = JsonConvert.DeserializeObject<DataTable>(jsonData); foreach (DataRow row in deserializedTable.Rows) { Console.WriteLine(row["Name"] + " " + row["Age"]); } } }
推荐阅读