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?