Can anyone tell me how can I add a new student? I began to learn to work with json. I tried to read, delete or rename something in json. It works well, but i have problem with add a new student.. :
StreamReader input = new StreamReader("student.txt");
string kontext = input.ReadToEnd();
input.Close();
JSONNode j = JSONNode.Parse(kontext);
Console.WriteLine("ID: ");
string c = "\"" + Console.ReadLine() + "\"";
//write to console name and surname by id
int n = j["student"].Count;
for (int i = 0; i < n; i++)
{
string temp = j["student"][i][0].ToString("");
if(c == temp)
{
Console.WriteLine(j["student"][i]["name"]);
Console.WriteLine(j["student"][i]["surname"]);
}
}
//rename by id + save to json
Console.WriteLine("ID: ");
c = "\"" + Console.ReadLine() + "\"";
for (int i = 0; i < j["student"].Count; i++)
{
string temp = j["student"][i][0].ToString("");
if (c == temp)
{
Console.Write("New name: ");
string rename = Console.ReadLine();
j["student"][i]["meno"] = rename;
StreamWriter output = new StreamWriter("student.txt");
output.WriteLine(j.ToString(""));
output.Close();
Console.WriteLine(j["student"][i]["meno"]);
}
}
//remove by id
Console.WriteLine("ID: ");
c = "\"" + Console.ReadLine() + "\"";
for (int i = 0; i < j["student"].Count; i++)
{
string temp = j["student"][i][0].ToString("");
if (c == temp)
{
j["student"].Remove(i);
StreamWriter output = new StreamWriter("student.txt");
output.WriteLine(j.ToString(""));
output.Close();
}
}
//add new student
Console.ReadKey();
}
Here is my response