6

How would i keep appending data? I have this:

{
  "13232": [
    "2012952"
  ]
}

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

{
  "13232": [
    "2012952"
  ],
  "19213": [
    "2016086"
  ]
}

This is the code i use:

JArray array = new JArray();
array.Add(Itemid);

JObject o = new JObject();
o[Userid] = array;

string json = o.ToString(Formatting.Indented);
//i know this keeps appending text but how would i append it inside the { and }?
File.AppendAllText("E:/media.json", json);

I literally have no idea how to keep adding it, but maybe someone else has?

1
  • 1
    You need to read data from existing file, deserialize it from JSON to JArray, append your new data to this JArray and overwrite old file with updated data. Commented Apr 20, 2017 at 20:38

4 Answers 4

7

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)
Sign up to request clarification or add additional context in comments.

Comments

1

The safest approach is read-update-rewrite (applies to JSON and XML format as they don't support appending).

Next option if you can live with invalid JSON is to simply concatenate JSON fragments with code you have and than use SupportMultipleContent in JsonReader to read fragments Read Multiple Fragments With JsonReader

If those approaches don't work and your format is fixed - find position of last ] in the file, seek stream there and write new array elements and append ]}.

Comments

1

I recommand you use Newtonsoft Json lib, available as a nuget package.

You can the make a model class to represent on of the json node then you can de-serialize you Json to that model and build an array containing the new element at the end to then re-serialize it to json after.

Look at this MSDN page about it: https://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx

Edit: Actual NewtonSoft Documentation

In steps: 1 Deserialize the collection

2: And a new class instance with listName.Add(className);

3: Reserialize the collection

Comments

0

My approach was to add an additional step to my string handling.

public void WriteObjectToFile(object objectToInsert)
{
var stringToSaveToFile = JsonConvert.SerializeObject(objectToInsert) + ",";
File.AppendAllText(this.path, stringToSaveToFile); 
}

public List<objects> GetListFromFile()
{
var notQuiteJSONstring = File.ReadAllTest(this.path);
var treatment1 = notQuiteJSONstring.Substring(0, notQuiteJSONstring.Length -1); //remove the trailing comma
var treatment2 = "[" + treatment1 + "]"; // wrap string with brackets to deserialize as list

var list = JsonConvert.DeserializeObject<List<object>>(treatment2);
return list;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.