Sunday, August 25, 2013

Generate JSON File from database data using c#

Following code will help you to generate JSON file from database table.

//Get records from database
var products = db.Products.ToList();

//Generate JSON from database data
using (StringWriter writer = new StringWriter())
    // Json.Write sends a Json-encoded string to the writer.
    System.Web.Helpers.Json.Write(products, writer);
    // When ready, you can send the writer
    // output to the browser, a file, etc.
    /*Uncomment above line to view JSON
       output in browser*/

    using (StreamWriter outfile =
               new StreamWriter(@"c:\Temp\Products.json"))

Please note: In order to have this code work, you will need to have "System.web.Helpers" dll added to your solution.

No comments:

Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:

Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape