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; }
        }
    }
}
原文地址:https://www.cnblogs.com/xiaoqiyaozou/p/15692788.html