How to write to a JSON file using C#?

时间秒杀一切 提交于 2019-12-11 04:47:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!