问题
I want to be able to add users to a Users json file. I'm using Json.net to do this, anyone got any ideas of how I might go about doing this?
回答1:
Here is a simple way of writing a JSON file in c#
UserData user = Call Method Here
string json = JsonConvert.SerializeObject(user, Formatting.Indented);
File.WriteAllText(@"c:\user.json", json);
This will write a simple JSON file and save it to your computer.
回答2:
Assuming that you have to add users to an existing JSON file, the basic approach is:
- Read / parse JSON file to get an in-memory data structure
- Modify the data structure to add the users
- Unparse / write the data structure to a new copy of the file.
There is no way to do update-in-place on a JSON file.
来源:https://stackoverflow.com/questions/22536818/how-to-write-to-a-json-file-using-c