using System.Collections.Generic; using System.IO; using Newtonsoft.Json; namespace BoreholeExtract { public class ConfigService { private const string FileName = "config.json"; // 加载配置 public List LoadConfig() { if (!File.Exists(FileName)) return new List(); try { var json = File.ReadAllText(FileName); // 使用 Newtonsoft 反序列化 // 即使 JSON 格式稍微有点错(比如多了个逗号),Newtonsoft 通常也能兼容 var result = JsonConvert.DeserializeObject>(json); return result ?? new List(); } catch { // 文件损坏或格式错误,返回空列表,稍后逻辑会填充默认值 return new List(); } } // 保存配置 public void SaveConfig(IEnumerable configs) { // 设置格式化选项:缩进排版,方便人眼阅读 var json = JsonConvert.SerializeObject(configs, Formatting.Indented); File.WriteAllText(FileName, json); } } }