将数据附加到JSON文件C#winforms

时间:2018-03-19 12:41:14

标签: c# json winforms

我想将数据附加到JSON文件中,我不知道如何执行此操作。我尝试过使用牛顿软件,但我无法使用它。

JSON文件:

[
  {
    "Name": "Banana",
    "Calories": "88,7",
    "Country": "Africa",
    "Longtitude": 12123,
    "Latitude": 12345
  },
  {
    "Name": "Pineapple",
    "Calories": "88,7",
    "Country": "South America",
    "Longtitude": 12123,
    "Latitude": 12345
  }
]

我的课程:

public class Smoothie
    {
        public string Name { get; set; }
        public string Calories { get; set; }
        public string Country { get; set; }
        public double Longtitude { get; set; }
        public double Latitude { get; set; }
    }

我如何尝试添加它:

private void button1_Click(object sender, EventArgs e)
        {
            Smoothie smoothie = new Smoothie();
            smoothie.Name = inpName.Text;
            smoothie.Calories = inpCalories.Text;
            smoothie.Country = inpCountry.Text;
            smoothie.Longtitude = double.Parse(inpLong.Text);
            smoothie.Latitude = double.Parse(inpLat.Text);

            using (StreamWriter file = File.CreateText("info.json"))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(file, smoothie);
            }
}

如何正确地将数据添加到JSON文件?我也收到错误消息,说info.json已被其他进程使用。

0 个答案:

没有答案