Share via


Delete all data from json file by c#

Question

Tuesday, March 27, 2018 1:47 PM

Hi All,

           In my asp.net application i am using json type file. i storing some kind of user data based on user.  when i select user A i need to store user A information.  If i want to store user B information i want to remove  data of user  A and store user B. How its possible?

All replies (5)

Wednesday, March 28, 2018 6:04 AM ✅Answered

Why not delete the file you don't want and make a new one?

Or read the file, remove the User A stuff, add User B stuff, write file.

Do you have a more specific description of what you are unable to do?  Do you know how to read a file?  Do you know how to write a file?  Do you know how to delete a file?


Wednesday, March 28, 2018 7:20 AM ✅Answered

Hi binustrat,

It seems you want to store some users' information in a json file. When storing new information in the json file, delete the old information.

In order to store data in json file, we can use Json.NET. When we use File.WriteAllText, it will cover the existing data in this file. See this demo:

public class InformationViewModel
{
    public string Name { get; set; }
    public string Address { get; set; }
    public int Age { get; set; }
}
public ActionResult Index(string name)
{
    InformationViewModel vm = new InformationViewModel();
    if (name == "A")
    {

        vm.Name = name;
        vm.Address = "A's address";
        vm.Age = 21;
    }
    else if(name == "B")
    {
        vm.Name = name;
        vm.Address = "B's address";
        vm.Age = 23;
    }
    List<InformationViewModel> list = new List<InformationViewModel>();
    list.Add(vm);
    string json = JsonConvert.SerializeObject(list.ToArray());
    System.IO.File.WriteAllText(@"E:\Daisy\JsonFile.json", json);
    return View();
}

Best Regards,

Daisy


Tuesday, March 27, 2018 1:52 PM

This is some kind of temporary storage ? You won't have conflicts if a user select user A and another user select user B on the same page ? For now it seems you could just load the data you need and save that to the json file overwriting previous data ?

Or if you necessarily have things left over from a previous selection, you'll have to select data not related to the current user and delete them explicitely but it would seem weird...

You don't use json as a replacement for a "real" database ?


Tuesday, March 27, 2018 2:46 PM

yes This is temporary storage only. I just want to replace user A data with user B i json file


Tuesday, March 27, 2018 3:00 PM

Still not 100% sure to get your design. If you don't need anything from the previous file (you asked for deleting all data from a json file ?), it seems to me you could just select data for user B from your source and just overwrite the previous file ???

If you need to keep things from the previous file, I would load the json data, would update the objects in memory as needed and would save again that to the json file.

If you need further help, you'll likely need to explain your design which seems a bit "suspect" for now (it seems you have some room for conflicting access to a single file if two users are using that on two different users).