zoukankan      html  css  js  c++  java
  • 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; }
            }
        }
    }
  • 相关阅读:
    常见的 常见的 User_Agent
    scp 传送数据的几种方式
    最近面试的几个问题 (三) io 多路复用
    mac 磁盘分区 时间胶囊创建失败
    最近面试的几个问题 (二) request_url 去重之布隆去重
    最近面试的几个问题 (一) 进程和线程、协程的区别
    chromedriver与chrome最新版本对应表 转自-河岸上的酸菜鱼 简书地址-https://www.jianshu.com/u/bbea92f78aca
    Sql代码美化工具:Sql Pretty Printer for SSMS V3.6.1
    如何给非理科生讲明白什么是云计算、大数椐(下)
    如何给非理科生讲明白什么是云计算、大数椐(上)
  • 原文地址:https://www.cnblogs.com/xiaoqiyaozou/p/15692788.html
Copyright © 2011-2022 走看看