//This is what I have implemented. This works fine. But the problem, when I again submit the data, the old data remove. Only the last inserted data shown in file. I want all the data in Json file when we save.
If anyone has idea, please help me.
public ActionResult Index(UserData model)
{
try
{
// Pass the "personlist" object for conversion object to JSON string
string jsondata = new JavaScriptSerializer().Serialize(model);
string path = Server.MapPath("~/App_Data/output.json");
// Write that JSON to txt file,
System.IO.File.WriteAllText(path , jsondata);
TempData["msg"] = "Json file Generated! check this in your App_Data folder";
}
catch (Exception e) { }
return RedirectToAction("Index");
}
FIle.AppendAllTextbut that will produce an invalid JSON file - JSON can't have multiple root elements.JavaScriptSerializercan do that. Everyone, including Microsoft, is using Json.NET instead, with egJsonConvert.SerializeObject(model, Formatting.None), then useAppendLineto add a new line to the file