HI all,
Existing JSon File Contains and File Saved as UniqueID.JSON
{
UniqueID :1123,
Datetime:"",
User:"",
}
And I need to read the file and append the courses (It's List) taken by the user into Existing Json File.Please help me
Expected output
{
UniqueID :1123,
Datetime:"",
User:"",
Courses:[
{
name : English
},
{
name : Maths
},
]
}

Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 26, 2023, 10:44 AM EDT
Try this.
using System; using System.Collections.Generic; using System.IO; using Newtonsoft.Json; class Program { static void Main() { string json = "ExistingJsonData"; var data = JsonConvert.DeserializeObject(json); // Initialize the Courses list data.Courses = data.Courses ?? new List(); var courses = new List { new Course { Name = "English" }, new Course { Name = "Maths" } }; data.Courses.AddRange(courses); string updatedJson = JsonConvert.SerializeObject(data, Formatting.Indented); Console.WriteLine("List data appended to the JSON data successfully."); } } class Data { public int UniqueID { get; set; } public string Datetime { get; set; } public string User { get; set; } public List Courses { get; set; } // Constructor to initialize the Courses list public Data() { Courses = new List(); } } class Course { public string Name { get; set; } }Amit MohantyRank #112 · 20K reputationPosted Jun 26, 2023, 5:17 AM EDT
Check this:
using System; using System.Collections.Generic; using System.IO; using Newtonsoft.Json; class Program { static void Main() { string json = "ExistingJsonData"; var data = JsonConvert.DeserializeObject(json); var courses = new List { new Course { Name = "English" }, new Course { Name = "Maths" } }; data.Courses.AddRange(courses); string updatedJson = JsonConvert.SerializeObject(data, Formatting.Indented); Console.WriteLine("List data appended to the JSON data successfully."); } } class Data { public int UniqueID { get; set; } public string Datetime { get; set; } public string User { get; set; } public List Courses { get; set; } } class Course { public string Name { get; set; } }Drt82 reputationPosted Jun 26, 2023, 9:53 AM EDT
Hi Amit
data.Courses.AddRange(courses);
While adding am getting System.NullReferenceException: 'Object reference not set to an instance of an object.' but when i debug having data .
Drt82 reputationPosted Jun 26, 2023, 8:46 PM EDT
Hi!!
var courses = new List { new Course { Name = "English" }, new Course { Name = "Maths" } }; If i want to add the courses through dynamically how should i append into courses. Trying to Append but it replace the old content but not new course is not appending
Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 27, 2023, 1:02 AM EDT
using System; using System.Collections.Generic; using System.IO; using Newtonsoft.Json; class Program { static void Main() { string json = "ExistingJsonData"; var data = JsonConvert.DeserializeObject(json); // Initialize the Courses list if it's null data.Courses = data.Courses ?? new List(); var courses = new List { new Course { Name = "English" }, new Course { Name = "Maths" } }; data.Courses.AddRange(courses); string updatedJson = JsonConvert.SerializeObject(data, Formatting.Indented); Console.WriteLine("List data appended to the JSON data successfully."); } } class Data { public int UniqueID { get; set; } public string Datetime { get; set; } public string User { get; set; } public List Courses { get; set; } // Constructor to initialize the Courses list public Data() { Courses = new List(); } } class Course { public string Name { get; set; } }Amit MohantyRank #112 · 20K reputationPosted Jun 27, 2023, 1:29 AM EDT
Hey, if you're getting a NullReferenceException when trying to add the courses to data.Courses, it means that the Courses property is null. You need to initialize it with a new instance of List before adding items to it.
using System; using System.Collections.Generic; using System.IO; using Newtonsoft.Json; class Program { static void Main() { string json = "ExistingJsonData"; var data = JsonConvert.DeserializeObject(json); var courses = new List { new Course { Name = "English" }, new Course { Name = "Maths" } }; // Initialize the Courses property if it's null if (data.Courses == null) { data.Courses = new List(); } data.Courses.AddRange(courses); string updatedJson = JsonConvert.SerializeObject(data, Formatting.Indented); Console.WriteLine("Courses appended to the JSON file successfully."); } } class Data { public int UniqueID { get; set; } public string Datetime { get; set; } public string User { get; set; } public List Courses { get; set; } } class Course { public string Name { get; set; } }