C# 读取json格式文件_c# 读取json文件

读取json格式文件

安装 Newtonsoft.Json 程序集

1. 选择界面下方的【程序包管理器控制台】页面,输入安装指令

Install-Package Newtonsoft.Json

  2. 安装完成后,请确保在代码文件的顶部包含以下 using 指令:

using Newtonsoft.Json;

创建读取Json文件的类

建立一个ReadJson.cs文件,内容如下:

  1. using System.Collections.Generic;
  2. namespace SplashScreen
  3. { internal class ReadJson
  4. {
  5. public List<int> y1_value { get; set; }
  6. public List<int> y2_value { get; set; }
  7. public List<int> y3_value { get; set; }
  8. public List<int> y4_value { get; set; }
  9. public List<int> y5_value { get; set; }
  10. public List<int> y6_value { get; set; }
  11. public List<int> y7_value { get; set; }
  12. public List<int> y8_value { get; set; }
  13. public List<int> y9_value { get; set; }
  14. }
  15. }

新建GUI界面

添加一个按钮和一个ListBox控件

 在Form1.cs中添加如下代码:

设置全局变量:

  1. List<int> y1_value = new List<int>();
  2. List<int> y2_value = new List<int>();
  3. List<int> y3_value = new List<int>();
  4. List<int> y4_value = new List<int>();
  5. List<int> y5_value = new List<int>();
  6. List<int> y6_value = new List<int>();
  7. List<int> y7_value = new List<int>();
  8. List<int> y8_value = new List<int>();
  9. List<int> y9_value = new List<int>();

读取文件按钮点击事件

  1. private void read_json_Click(object sender, EventArgs e)
  2. {
  3. string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Y_value.json");
  4. ReadJsonFile(fileName);
  5. }

读取文件函数

  1. private void ReadJsonFile(string fileName)
  2. {
  3. if (File.Exists(fileName))
  4. {
  5. string json = File.ReadAllText(fileName);
  6. ReadJson data = JsonConvert.DeserializeObject<ReadJson>(json);
  7. listBox1.Items.Add(AppDomain.CurrentDomain.BaseDirectory);
  8. this.y1_value = data.y1_value;
  9. this.y2_value = data.y2_value;
  10. this.y3_value = data.y3_value;
  11. this.y4_value = data.y4_value;
  12. this.y5_value = data.y5_value;
  13. this.y6_value = data.y6_value;
  14. this.y7_value = data.y7_value;
  15. this.y8_value = data.y8_value;
  16. this.y9_value = data.y9_value;
  17. List<List<int>> yValues = new List<List<int>>()
  18. {
  19. y1_value, y2_value, y3_value, y4_value, y5_value,
  20. y6_value, y7_value, y8_value, y9_value
  21. };
  22. for (int i = 0; i < yValues.Count; i++)
  23. {
  24. string prefix = $"y{i + 1}";
  25. string line = string.Join(" ", yValues[i]);
  26. listBox1.Items.Add($"{prefix}: {line}");
  27. }
  28. }
  29. }

制作一个模拟的启动界面

新建一个Form,命名为SplashScreen。

不显示任务栏图标

程序置于屏幕中央

取消窗体边框

 设置窗体尺寸,可以设为与图片尺寸相同,也可以设置图片的显示方式

 添加图片框控件,并设置布局Dock为填充 

添加资源文件

选中项目,右键选择属性

属性窗口中选择资源那一栏,选择图像,添加资源——>添加现有文件

图片控件导入图片

在界面下方添加一个Panel控件,设置Dock属性为Bottom

 再添加一个Panel控件,放在Panel1控件里面,左侧

设置两个Panel的背景颜色

 添加一个定时器控件

 添加定时器的响应事件代码

  1. private void timer1_Tick(object sender, EventArgs e)
  2. {
  3. panel2.Width += 5;
  4. if (panel2.Width >= panel1.Width)
  5. {
  6. timer1.Stop();
  7. this.DialogResult = DialogResult.OK;
  8. this.Close();
  9. }
  10. }

 启动界面加载完成后加载主程序界面

方法一:Program.cs中添加如下代码:

  1. SplashScreen splashScreen = new SplashScreen();
  2. if(splashScreen.ShowDialog() == DialogResult.OK)
  3. {
  4. Application.Run(new Form1());
  5. }
  6. else
  7. Application.Exit();

 方法二:

  1. using System;
  2. using System.Windows.Forms;
  3. namespace SplashScreen
  4. {
  5. public partial class SplashScreen : Form
  6. {
  7. private Form1 form1;
  8. public SplashScreen()
  9. {
  10. InitializeComponent();
  11. form1 = new Form1();
  12. }
  13. private void timer1_Tick(object sender, EventArgs e)
  14. {
  15. panel_progressbar.Width += 5;
  16. if (panel_progressbar.Width >= panel_contant.Width)
  17. {
  18. timer1.Stop();
  19. this.Hide();
  20. form1.ShowDialog();
  21. this.Close();
  22. }
  23. }
  24. private void SplashScreen_Load(object sender, EventArgs e)
  25. {
  26. timer1.Start();
  27. }
  28. }
  29. }

 方法二:

启动界面代码如下:

  1. using System;
  2. using System.Windows.Forms;
  3. namespace SplashScreen
  4. {
  5. public partial class SplashScreen1 : Form
  6. {
  7. private Form1 form1;
  8. public SplashScreen1()
  9. {
  10. InitializeComponent();
  11. form1 = new Form1();
  12. }
  13. private void timer1_Tick(object sender, EventArgs e)
  14. {
  15. panel2.Width += 5;
  16. if (panel2.Width >= panel1.Width)
  17. {
  18. timer1.Stop();
  19. this.Hide();
  20. form1.ShowDialog();
  21. this.Close();
  22. }
  23. }
  24. private void SplashScreen_Load(object sender, EventArgs e)
  25. {
  26. timer1.Start();
  27. }
  28. }
  29. }

 Program.cs中代码

参考:C# Winform 现代化扁平化启动界面设计——自定义进度条