Append data in a json file in C#

前端 未结 3 2018
庸人自扰
庸人自扰 2020-12-07 02:51

How would i keep appending data? I have this:

{
  \"13232\": [
    \"2012952\"
  ]
}

And i want to add an another object to it, example:

3条回答
  •  执念已碎
    2020-12-07 03:04

    You won't be able to use file append operations to do this. File append operations can only add text to the end, they can't insert text at some point in the middle. This makes it impossible to use file-append to keep the JSON valid.

    You have two choices that I can think of:

    1. Read the entire file into an object, add your object, and then rewrite the entire file (poor performance)
    2. Open the file read/write, parse through until you get to the closing curly brace, then write the remaining data, then write the close curly brace (not trivial)

提交回复
热议问题