C#将项添加到JSON

时间:2015-04-13 11:24:38

标签: c# json

有谁能告诉我如何添加新学生? 我开始学习与json合作。我试图在json中读取,删除或重命名某些内容。它运作良好,但我有添加新学生的问题..:

        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();
    }

以下是我的回复 enter image description here

1 个答案:

答案 0 :(得分:0)

所以在你的提示后我做了这样的事情:

        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;}
}

这是我的json代码:

[{ “ID”: “54865”, “名称”: “帕沃尔”, “姓”: “Masny”, “住址”: “Blava酒店”, “日期”: “1990年12月1日”, “地点” : “科希策”}]

但是除了异常:附加信息:无法将当前JSON数组(例如[1,2,3])反序列化为类型'testjson.student',因为该类型需要一个JSON对象(例如{“name”:“value” })正确反序列化。任何提示如何解决?