I want to save data in local json file in this format:
[
{"Name":"sdafdsf","Password":"dsfads","FirstName":"fsdf","LastName":"dsfdafas"},
{"Name":"sddafdsf","Password":"dsfadds","FirstName":"fdsdf","LastName":"dsfdafasdfs"}
]
I am using this in the controller to serialize into json format:
public ActionResult Index(demo obj)
{
String json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
string path = Server.MapPath("~/app/");
//// Write that JSON to txt file,
//var read = System.IO.File.ReadAllText(path + "output.json");
System.IO.File.WriteAllText(path + "output.json", json);
return View();
}
This is my model:
public class demo
{
public string Name { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
But instead of proper JSON format I am getting this in my output.json file:
{"Name":"sdafdsf","Password":"dsfads","FirstName":"fsdf","LastName":"dsfdafas"}{"Name":"adfsdsfsafdsafasdfsdfsadf","Password":"dfsaasdsa","FirstName":"safd","LastName":"dfsafads"}
How can I save data in the proper format?
List<demo>would be easy.ActionResultis called only once when you have a completed array ofdemoto pass to it.