C# 读取json 文件 解析处理并另存
1、代码
// See https://aka.ms/new-console-template for more information using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; namespace Application { class Program { static void Main(string[] args) { string jsonPath = "E:/Code/tool/monitor.json"; string jsonStr = GetJsonStr(jsonPath); var monitor = GetJson(jsonStr); string jsonPathWS = "E:/Code/tool/ws.json"; var wsStr = GetJsonStr(jsonPathWS, false); var ws = GetJson(wsStr); var points = GetListPoint(ws); var jArray = (JArray)monitor; SetJsonValue(jArray, points); Console.WriteLine("Hello World!"); } /*** * 反序列化成json 对象 */ public static object GetJson(string jsonStr) { return JsonConvert.DeserializeObject(jsonStr); } /** * 获取点坐标 */ public static List<Point> GetListPoint(object o) { List<Point> points = new List<Point>(); JObject jObj = (JObject)o; var fetures = jObj["features"]; foreach (var item in fetures) { var p = new Point(); p.X = double.Parse(item["geometry"]["x"].ToString()); p.Y = double.Parse(item["geometry"]["y"].ToString()); p.EXPNO =item["attributes"]["EXPNO"].ToString(); points.Add(p); } return points; } /** * 设置json 并保存成新的json 文件 */ public static string SetJsonValue(JArray jArray, List<Point> points) { Random random = new Random(); List<int> listIndex = new List<int>(); List<Object> list = new List<Object>(); foreach (var item in jArray) { var tmp = item; if (tmp["name"]?.ToString() == "窨井水位") { foreach (var item2 in tmp["list"]) { var index = random.Next(points.Count); while (listIndex.Contains(index)) { index = random.Next(points.Count); } item2["BMX"] = points[index].X; item2["BMY"] = points[index].Y; item2["EXPNO"]=points[index].EXPNO; } } else if (tmp["name"]?.ToString() == "流量") { foreach (var item2 in tmp["list"]) { var index = random.Next(points.Count); while (listIndex.Contains(index)) { index = random.Next(points.Count); } item2["BMX"] = points[index].X; item2["BMY"] = points[index].Y; } } list.Add(tmp); } SaveJsonFile(JsonConvert.SerializeObject(list)); return ""; } /** * 保存新的文件 */ public static void SaveJsonFile(JToken jToken) { string jsonPath = "E:/Code/tool/monitor" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".json"; File.WriteAllText(jsonPath, jToken.ToString()); } /** * 读取json 文件 */ public static string GetJsonStr(string jsonPath, bool isCheck = true) { string line; string res = ""; using (StreamReader sr = new StreamReader(jsonPath)) { if (isCheck) { while ((line = sr.ReadLine()) != null) { //这一行值有问题 存在得时候转换json 失败 if (line.ToString().IndexOf("ADDTIME") <= 0) res = res + line.ToString().Trim(); } } else { res = sr.ReadToEnd(); } } return res; } public class Point { public double X { get; set; } public double Y { get; set; } public string EXPNO { get; set; } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!