-1

For a restaurant project, I need to be able to do this: The user would need to input his name, time, date, etc as a string, then using an ID adding that info to a JSON file, meaning the data would contain all the reservation data and could be deleted using that one ID? I have looked around but couldn't get anything to work with my multi cs file project.

This is what I thought of for the JSON file, a single line of strings, and being able to get deleted reading the "ID: 0001" part?

{
"Reservation":"ID: 0001\nName: John Doe\n", "ID: 0002\nName: Jane Doe"
}

But also needing to append it, when you're done making a reservation, it would come out as one large string and append it (if the id is different), but is it possible? I've seen the read-update-rewrite method, but I'm not sure I can use commas or something similar to keep parts seperated from, or that it has to be added under each other with one main object, which I've also seen.

[
  {
    "Id": 1,
    "SSN": 123,
    "Message": "whatever"

  },
  {
   "Id": 2,
    "SSN": 125,
    "Message": "whatever"
  }
]

Not exactly sure what is handy and what is not, I could easily seperate the different pieces of data but I'd want to keep it using the ID system.

Here's my overview code, so what I thought was deserialize > add or delete if user wants it (convert it to a string, append it with the string or delete parts) > serialize and save it again

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.Json;

namespace Noodle
{

    class ResOverzicht : Step 
    {
        public class AdminOverzichtReservering
        {
            
            public string Overzicht { get; set; }
        }

        public AdminOverzichtReservering Deserialize()
        {
            string json = File.ReadAllText("AdminOverzichtReservering.json");
            var AdminOverzichtReserveringJson = JsonSerializer.Deserialize<AdminOverzichtReservering>(json);
            return AdminOverzichtReserveringJson;
        }

        public override void Show()
        {
            Log("");
            var greetingsJson = new WelcomePage();
            string taalSetting = greetingsJson.Getlanguage(); 
            ConsoleKeyInfo input;

            do
            {
                {
                    Console.Clear();
                    var AdminOverzichtReserveringJson = Deserialize();
                    if (taalSetting == "nl")
                    { 
                        Console.WriteLine("Hier is het overzicht van de reserveringen: ");
                        Console.WriteLine(AdminOverzichtReserveringJson.Overzicht);
                    }

                    else
                    {
                        Console.WriteLine("Here is the overview of the reservations: ");
                        Console.WriteLine(AdminOverzichtReserveringJson.Overzicht);
                    }

                    input = Console.ReadKey(); //maybe after this it converts the string in the json to a c# string which can be changed and after that it rewrites the new string when you're done, with more or less data

                }
            }
            while (input.Key != ConsoleKey.Escape);

            if (input.Key == ConsoleKey.Escape)

                var Admin = new Admin();
                Admin.Show();

            }
        }
        public void Serialize(ResOverzicht resoverzicht)
        {
            var serializeOptions = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            var AdminOverzichtReserveringJson = JsonSerializer.Serialize(resoverzicht, serializeOptions);
            File.WriteAllText("AdminOverzichtReservering.json", AdminOverzichtReserveringJson);
        }
    }
}

I hope someone has an answer to this, I've seen How to write a JSON file in C#? , but I couldn't get the answer working, perhaps due to the classes or something?

  • 2
    Trying to use a JSON file as a database has inherent challenges because that's not what it's designed for, not to mention issues with appending data (hence formats like NDJSON). Do you really need it to be a JSON file or could you be using a database like SQLite for storage? Even with a database, you can query and then emit JSON outputs if required. https://github.com/praeclarum/sqlite-net – Adam May 17 '22 at 21:23
  • JSON is sadly required, so I couldn't use anything else. Probably why it's been so difficult getting this problem to work. – yourickmovies May 17 '22 at 21:36
  • 1
    Even in cases where you need to *produce* JSON, you don't necessarily need to *store* the working data as JSON. It's a not unusual paradigm to store the data in something like the aforementioned SQLite database, then use the JSON serializer when you need to write the content out to a JSON file – Adam May 17 '22 at 21:39
  • The front-end data returned by the server can be in json format, and is parsed through c# code. Is there a corresponding api to simplify the operation by adding and deleting json files through vs? – Housheng-MSFT May 18 '22 at 08:53
  • I've finally solved the problem on my own using JsonWriter, basically grabbing the object from the JSON, put it in a string and change it, and then it rewrites it. – yourickmovies May 20 '22 at 14:12

0 Answers0