如何使用json.net将c#通用列表转换为json?
发布时间:2020-12-16 19:47:53 所属栏目:百科 来源:网络整理
导读:我将我的datatable转换为c#通用列表。 DataTable dt = mydata(); ListDataRow list = dt.AsEnumerable().ToList(); 现在我如何使用json.net将此列表转换为json?任何建议。 json格式的样本应该是这样的, {"Table" : [{"userid" : "1","name" : "xavyTechnol
我将我的datatable转换为c#通用列表。
DataTable dt = mydata(); List<DataRow> list = dt.AsEnumerable().ToList(); 现在我如何使用json.net将此列表转换为json?任何建议。 json格式的样本应该是这样的, {"Table" : [{"userid" : "1","name" : "xavyTechnologies","designation" : "","phone" : "9999999999","email" : "test@test.com","role" : "Admin","empId" : "","reportingto" : ""},{"userid" : "2","name" : "chendurpandian","designation" : "softwaredeveloper","phone" : "9566643707","email" : "chendur.pandia@gmail.com","role" : "Super User","empId" : "1","reportingto" : "xavyTechnologies"},{"userid" : "3","name" : "sabarinathan","designation" : "marketer","phone" : "66666666666","email" : "bala3569@gmail.com","role" : "User","empId" : "2","reportingto" : "chendurpandian"}]}
这里有一个例子:
using System; using System.Data; using Newtonsoft.Json.Linq; class Test { static void Main() { DataTable table = new DataTable(); table.Columns.Add("userid"); table.Columns.Add("phone"); table.Columns.Add("email"); table.Rows.Add(new[] { "1","9999999","test@test.com" }); table.Rows.Add(new[] { "2","1234567","foo@test.com" }); table.Rows.Add(new[] { "3","7654321","bar@test.com" }); var query = from row in table.AsEnumerable() select new { userid = (string) row["userid"],phone = (string) row["phone"],email = (string) row["email"] }; JObject o = JObject.FromObject(new { Table = query }); Console.WriteLine(o); } } 文件:LINQ to JSON with Json.NET (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |