【问题标题】:C# Add item to JSON [duplicate]C#将项目添加到JSON [重复]
【发布时间】:2015-04-13 11:24:37
【问题描述】:

谁能告诉我如何添加新学生? 我开始学习使用 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();
    }

这是我的回复

【问题讨论】:

  • 您可以将您的 json 添加为代码而不是图片吗?
  • 您似乎正在努力完成作业。你能说出你尝试了什么吗?
  • 为什么不使用真正的 C# 对象对数据进行建模,并使用 Newtonsoft Json.net newtonsoft.com/json 之类的东西对它们进行序列化/反序列化?

标签: c# json


【解决方案1】:

所以根据你的提示,我做了这样的事情:

        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","name":"Pavol","surname":"Masny","adress":"Blava","date":"12.1.1990","place" :"科希策"}]

但有例外:附加信息:无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“testjson.student”,因为该类型需要 JSON 对象(例如 {"name":"value" }) 正确反序列化。有什么解决办法吗?

【讨论】:

  • 有趣的命名约定...
猜你喜欢
  • 1970-01-01
  • 2020-08-21
  • 2013-03-24
  • 2015-07-10
  • 2018-06-18
  • 2021-12-24
  • 2018-10-16
  • 2018-12-11
  • 1970-01-01
相关资源
最近更新 更多