How to convert c# generic list to json using json.net?

Here’s one example:

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", "[email protected]" });
        table.Rows.Add(new[] { "2", "1234567", "[email protected]" });
        table.Rows.Add(new[] { "3", "7654321", "[email protected]" });

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

Documentation: LINQ to JSON with Json.NET

Leave a Comment