-6

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 enter image description here

Michele d'Amico
  • 22,111
  • 8
  • 69
  • 76
Deyeth
  • 59
  • 2
  • 11

1 Answers1

0

so after your tips I did something like this:

        List<student> data = new List<student>();

        student std;
        using (StreamReader output = File.OpenText("student.txt")) 
        {
            JsonSerializer serializer = new JsonSerializer();
             std = (student)serializer.Deserialize(output, typeof(student));
        }
        data.Add(std);

        data.Add(new student()
        {
            id = "25628",
            name = "Marko",
            surname = "Polo",
            adress = "Prague",
            date = "15.1.1998",
            place = "Munchen"

        });

        string json = JsonConvert.SerializeObject(data.ToArray());

        System.IO.File.WriteAllText("student.txt", json);

class student
{
    public string id {get;set;}
    public string name {get;set;}
    public string surname {get;set;}
    public string adress {get;set;}
    public string date {get;set;}
    public string place{get;set;}
}

and here is my json code :

[{"id":"54865","name":"Pavol","surname":"Masny","adress":"Blava","date":"12.1.1990","place":"Kosice"}]

but theres Exception: Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'testjson.student' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. Any tips how to fix it?

Deyeth
  • 59
  • 2
  • 11