0

I am trying to get data from web form and convert properties into a list of json objects through json.net, first time a list of object is stored in a file. But next time, instead of appending data into list it creates new list. So how can I add it in same list. Here is my code

    public List<StudentProps> GetProps(StudentProps p)
    {
        List<StudentProps> s = new List<StudentProps>();
        s.Add(p);
        return s;
    }


    public void GetStudent(StudentProps p)
    {
        var json = JsonConvert.SerializeObject(GetProps(p), Formatting.Indented);
        String FilePath = @"C:\Users\Haseeb\Desktop\Test.json";
        File.AppendAllText(FilePath, json + Environment.NewLine);
        var ReadFile = File.ReadAllText(FilePath);

        // List<StudentProps> props = JsonConvert.DeserializeObject<List<StudentProps>>(ReadFile).ToList();

    }
Haseeb
  • 1
  • 1
  • 3
  • You can't easily append an item to an array inside a JSON file, see [json add new object to existing json file C#](https://stackoverflow.com/q/43529386). But if you are using [NDJSON](http://ndjson.org/) (multiple JSON fragments separated by newlines) you can append. Then to deserialize the entire thing see [What is the correct way to use JSON.NET to parse stream of JSON objects?](https://stackoverflow.com/q/26601594), and to re-serialize all the entries from scratch see [Serialize as NDJSON using Json.NET](https://stackoverflow.com/q/44787652). – dbc May 02 '19 at 22:51
  • In fact this may be a duplicate of those three questions, agree? – dbc May 02 '19 at 22:51

0 Answers0