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.
//Response.Write(writer);
/*Uncomment above line to view JSON
output in browser*/
using (StreamWriter outfile =
new StreamWriter(@"c:\Temp\Products.json"))
{
outfile.Write(writer.ToString());
}
}
Please note: In order to have this code work, you will need to have "System.web.Helpers" dll added to your solution.
No comments:
Post a Comment