Home > Mobile >  Writing multiple values to json file throws "only one top level item is allowed " error C#
Writing multiple values to json file throws "only one top level item is allowed " error C#

Time:01-03

I'm working on a queue management system app, when the person on the counter-press the call next button my program fetches the next person's details using their Id and save them to a JSON file which is then read by a page that displays their name,id, and picture. Now I am able to save a single data but when I save another value the JSON file says "only one top-level item is allowed"

   List<ServerToScreen> ScreenData = new List<ServerToScreen>()
            {
                new ServerToScreen()
                {
                    StudentID=StudentId,
                    Name = StudentDetails.StudentName,
                    Photo =StudentDetails.Photo!=null? Convert.ToBase64String( StudentDetails.Photo):NoProfilePic,
                    HallNo = Result.Where(x=>x.RegisNumber==StudentId).FirstOrDefault().HALLNumber,
                    TvIP = Result.Where(x=>x.RegisNumber==StudentId).FirstOrDefault().TVIPAddress,
                    IsCalledOnScreen=false
                    
                }
            };
            string ServerToJson = new JavaScriptSerializer().Serialize(ScreenData);
            string path = Server.MapPath("~/TvScreenData/");
            System.IO.File.AppendAllText(path   "output.json", ServerToJson);

This is the part of the code that writes the data to JSON file and here is the model i used for the same

 public class ServerToScreen {
      
        public string StudentID { get; set; }
        public string Name { get; set; }
        public string Photo { get; set; }
        public string HallNo { get; set; }
        public string TvIP { get; set; }
        public bool IsCalledOnScreen { get; set; }

    }

and the JSON it creates

[
  {
    "StudentID": "09292",
    "Name": "brad pit Aman",
    "Photo": "null",
    "HallNo": "1",
    "TvIP": "192.0.0.1",
    "IsCalledOnScreen": false
  }
]

[
  {
    "StudentID": "282828",
    "Name": "mani a mani",
    "Photo": "null",
    "HallNo": "1",
    "TvIP": "192.0.0.1",
    "IsCalledOnScreen": false
  }
]

now somewhere I read that I have to save the list as obj1,obj2 though I understand what it means I couldn't figure out how to do it, I'm really stuck so any help would be great, Thank you.

CodePudding user response:

The issue is caused by adding a new json list to the file every time this method gets called.

What you want to do is first load the json list from this file into your csharp code, then add the new ServerToScreen object to the loaded list, and then replace the json file with the new list.

Code example:

ServerToScreen newObject = new ServerToScreen() {
// create the new ServerToScreen object with all properties
}

var serializer = new JavaScriptSerializer();
string outputPath = Server.MapPath("~/TvScreenData/")   "output.json";

// Load current list from the file
string currentList = System.IO.File.ReadAllText(outputPath);
List<ServerToScreen> screenData = serializer.Deserialize<List<ServerToScreen>>(currentList);

// Add the new object to this list
screenData.Add(newObject);

// Serialize the list including the new object, and replace the file
string jsonWithNewObject = serializer.Serialize(screenData);
System.IO.File.WriteAllText(outputPath, jsonWithNewObject);

Note: code is untested

  • Related