How do I save a JSON file with four spaces indentation using JSON.NET?

后端 未结 3 1326
无人共我
无人共我 2020-12-15 22:59

I need to read a JSON configuration file, modify a value and then save the modified JSON back to the file again. The JSON is as simple as it gets:

{
    "         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 23:44

    The problem is that you are using config.ToString(), so the object is already serialised into a string and formatted when you write it using the JsonTextWriter.

    Use a serialiser to serialise the object to the writer instead:

    JsonSerializer serializer = new JsonSerializer();
    serializer.Serialize(jw, config);
    

提交回复
热议问题