当前位置:网站首页>使用Text.json解析Json文件

使用Text.json解析Json文件

2022-01-15 02:02:39 罗迪尼亚的熔岩

建立一个modbus变量类库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonPractice
{
    
    public class Variable_Modbus
    {
    
        //变量名称
        public string VarName {
     get; set; }
        //存储区域
        public string StoreArea {
     get; set; }
        //地址
        public string Address {
     get; set; }
        //数据类型
        public string DataType {
     get; set; }
        //是否归档 是否存入数据库
        public string IsFiling {
     get; set; }
        //是否报警
        public string IsAlarm {
     get; set; }
        //是否制作报表
        public string IsReport {
     get; set; }
        //变量注释说明
        public string Note {
     get; set; }
        //绝对地址
        private string absoluteAddress;
        //判断存储区
        public string AbsoluteAddress
        {
    
            get
            {
    
                int store = 0;
                switch (StoreArea)//通过存储区判断并显示存储位置
                {
    
                    case "01 Coil Status(0x)":
                        store = 0;
                        break;
                    case "02 Input Status(1x)":
                        store = 1;
                        break;
                    case "03 Holding Register(4x)":
                        store = 4;
                        break;
                    case "04 Input Registers(3x)":
                        store = 3;
                        break;
                    default:
                        store = 4; //默认值
                        break;
                }
                absoluteAddress = (store * 10000 + Convert.ToInt32(Address)).ToString();//30001,40001开始
                return absoluteAddress;
            }
            set
            {
    
                value = absoluteAddress;
            }
        }
    }
}

建立一个helper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text.Encodings.Web;

namespace Tool
{
    
    public static class JsonTool
    {
    
        public static string ToJson<T>(this T obj)
        {
    
            return JsonSerializer.Serialize(obj, typeof(T),
                new JsonSerializerOptions
                {
    
                    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                });
        }
    }
}

解析json

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Encodings.Web;
using Tool;
using System.IO;
using System.Xml;
//using Newtonsoft.Json;
using System.Text.Json;



namespace JsonPractice
{
    
    class Program
    {
    
        static void Main(string[] args)
        {
    
            Student stu = new Student() {
    Id=10001,Name="张三",Phonenumber="1505050505" };

            //var jsonstu = JsonSerializer.Serialize(stu, typeof(Student),
            //new JsonSerializerOptions
            //{
    
            // Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            //});

            //System.Text.Json.Utf8JsonReader
            //Console.WriteLine(JsonTool.ToJson(stu));
            //Console.ReadKey();
            //Student aa = JsonSerializer.Deserialize<Student>(fs, new JsonSerializerOptions
            //{
    
            // Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            //});
            //Console.WriteLine("{0},{1},{2}", aa.Id, aa.Name, aa.Phonenumber);
            //Console.ReadKey();

            //JsonSerializer.Serialize<Variable_Modbus>(fs, jsonstu,
            // new JsonSerializerOptions
            // {
    
            // Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            // });

            JsonSerializerOptions jso = new JsonSerializerOptions
            {
    
                Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            };
            
            string FilePath = "D:\\jsontxt.json";
            string FilePath2 = "D:\\Variable_Modbus.json";
            FileStream fs = new FileStream(FilePath2, FileMode.Open);

            JsonDocument doc = JsonDocument.Parse(fs);
            List<Variable_Modbus> VarModbusList = new List<Variable_Modbus>();

            var ip = doc.RootElement.GetProperty("Root").GetProperty("Variable")[0].GetProperty("VarName");

            foreach (var item in doc.RootElement.GetProperty("Root").GetProperty("Variable").EnumerateArray())
            {
    
                Variable_Modbus objVar = new Variable_Modbus();

                objVar.VarName = item.GetProperty("VarName").GetString();
                objVar.Address = item.GetProperty("Address").GetString();
                objVar.DataType = item.GetProperty("DataType").GetString();
                objVar.StoreArea = item.GetProperty("StoreArea").GetString();
                objVar.IsFiling = item.GetProperty("IsFiling").GetString();
                objVar.IsAlarm = item.GetProperty("IsAlarm").GetString();
                objVar.IsReport = item.GetProperty("IsReport").GetString();
                objVar.Note = item.GetProperty("Note").GetString();
                objVar.AbsoluteAddress = item.GetProperty("AbsoluteAddress").GetString();
                VarModbusList.Add(objVar);
            }

            for (int i = 0; i < VarModbusList.Count; i++)
            {
    
                Console.WriteLine(VarModbusList[i].Note);
            }

            //var age = doc.RootElement.GetProperty("Variable");
            Console.WriteLine(VarModbusList[0].Note);
            Console.ReadKey();

            /// 转换XML为json
            //XmlDocument doc = new XmlDocument();

            //FileStream fs = new FileStream(FilePath2, FileMode.Open);
            //doc.Load(fs);
            //string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
            //Console.WriteLine("XML -> JSON: {0}", json);
            //Console.ReadKey();

        }
    }
}

json文件

{
    
  "Root": {
    
    "Variable": [
      {
    
        "VarName": "LQT_Level",
        "Address": "0",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "冷却塔液位",
        "IsFiling": "1",
        "IsAlarm": "1",
        "IsReport": "1",
        "AbsoluteAddress": "40000"
      },
      {
    
        "VarName": "LQT_InPre",
        "Address": "2",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "冷却塔入口压力",
        "IsFiling": "1",
        "IsAlarm": "1",
        "IsReport": "1",
        "AbsoluteAddress": "40002"
      },
      {
    
        "VarName": "LQT_InTemp",
        "Address": "4",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "冷却塔入口温度",
        "IsFiling": "1",
        "IsAlarm": "1",
        "IsReport": "1",
        "AbsoluteAddress": "40004"
      },
      {
    
        "VarName": "LQT_OutPre",
        "Address": "6",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "冷却塔出口压力",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40006"
      },
      {
    
        "VarName": "LQT_OutTemp",
        "Address": "8",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "冷却塔出口温度",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40008"
      },
      {
    
        "VarName": "LQT_BSPre",
        "Address": "10",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "冷却塔补水压力",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40010"
      },
      {
    
        "VarName": "LQB1_Current",
        "Address": "12",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#冷却泵电流",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40012"
      },
      {
    
        "VarName": "LQB1_Fre",
        "Address": "14",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#冷却泵频率",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40014"
      },
      {
    
        "VarName": "LQB2_Current",
        "Address": "16",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#冷却泵电流",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40016"
      },
      {
    
        "VarName": "LQB2_Fre",
        "Address": "18",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#冷却泵频率",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40018"
      },
      {
    
        "VarName": "KYJ1_OutTemp",
        "Address": "20",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#空压机回水温度",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40020"
      },
      {
    
        "VarName": "KYJ2_OutTemp",
        "Address": "22",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#空压机回水温度",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40022"
      },
      {
    
        "VarName": "KYJ3_OutTemp",
        "Address": "24",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3#空压机回水温度",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40024"
      },
      {
    
        "VarName": "CQG1_OutPre",
        "Address": "26",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#储气罐出口压力",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40026"
      },
      {
    
        "VarName": "CQG2_OutPre",
        "Address": "28",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#储气罐出口压力",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40028"
      },
      {
    
        "VarName": "CQG3_OutPre",
        "Address": "30",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3#储气罐出口压力",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40030"
      },
      {
    
        "VarName": "Env_Temp",
        "Address": "32",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "露点温度",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40032"
      },
      {
    
        "VarName": "FQG_Temp",
        "Address": "34",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "分汽缸温度",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40034"
      },
      {
    
        "VarName": "FQG_Pre",
        "Address": "36",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "分汽缸压力",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40036"
      },
      {
    
        "VarName": "LQT_InPre_H",
        "Address": "38",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "冷却塔回水压力高限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40038"
      },
      {
    
        "VarName": "LQT_InTemp_H",
        "Address": "40",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "冷却塔回水温度高限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40040"
      },
      {
    
        "VarName": "LQT_OutPre_H",
        "Address": "42",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "冷却塔供水压力高限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40042"
      },
      {
    
        "VarName": "LQT_OutTemp_H",
        "Address": "44",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "冷却塔供水温度高限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40044"
      },
      {
    
        "VarName": "LQB1_Fre_H",
        "Address": "46",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#冷却泵频率高限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40046"
      },
      {
    
        "VarName": "LQB1_Fre_L",
        "Address": "48",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#冷却泵频率低限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40048"
      },
      {
    
        "VarName": "LQB2_Fre_H",
        "Address": "50",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#冷却泵频率高限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40050"
      },
      {
    
        "VarName": "LQB2_Fre_L",
        "Address": "52",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#冷却泵频率低限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40052"
      },
      {
    
        "VarName": "KYJ1_Fre_Set",
        "Address": "54",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#空压机频率给定",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40054"
      },
      {
    
        "VarName": "KYJ2_Fre_Set",
        "Address": "56",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#空压机频率给定",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40056"
      },
      {
    
        "VarName": "KYJ3_Fre_Set",
        "Address": "58",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3#空压机频率给定",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40058"
      },
      {
    
        "VarName": "FQG_Temp_H",
        "Address": "60",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "分汽缸温度高限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40060"
      },
      {
    
        "VarName": "FQG_Pre_H",
        "Address": "62",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "分汽缸压力高限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40062"
      },
      {
    
        "VarName": "Env_Temp_H",
        "Address": "64",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "露点温度高限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40064"
      },
      {
    
        "VarName": "Env_Temp_L",
        "Address": "66",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "露点温度低限",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40066"
      },
      {
    
        "VarName": "LQB1_Start_Ctl",
        "Address": "100",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#冷却泵启动",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40100"
      },
      {
    
        "VarName": "LQB1_Stop_Ctl",
        "Address": "101",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#冷却泵停止",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40101"
      },
      {
    
        "VarName": "LQB1_Run_State",
        "Address": "102",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#冷却泵运行状态",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40102"
      },
      {
    
        "VarName": "LQB2_Start_Ctl",
        "Address": "103",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#冷却泵启动",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40103"
      },
      {
    
        "VarName": "LQB2_Stop_Ctl",
        "Address": "104",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# 冷却泵停止",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40104"
      },
      {
    
        "VarName": "LQB2_Run_State",
        "Address": "105",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#冷却泵运行状态",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40105"
      },
      {
    
        "VarName": "KYJ1In_Open_Ctl",
        "Address": "106",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#空压机进液阀开启",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40106"
      },
      {
    
        "VarName": "KYJ1In_Close_Ctl",
        "Address": "107",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#空压机进液阀关闭",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40107"
      },
      {
    
        "VarName": "KYJ1In_Run_State",
        "Address": "108",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1#空压机进液阀状态",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40108"
      },
      {
    
        "VarName": "KYJ2In_Open_Ctl",
        "Address": "109",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#空压机进液阀开启",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40109"
      },
      {
    
        "VarName": "KYJ2In_Close_Ctl",
        "Address": "110",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#空压机进液阀关闭",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40110"
      },
      {
    
        "VarName": "KYJ2In_Run_State",
        "Address": "111",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#空压机进液阀状态",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40111"
      },
      {
    
        "VarName": "KYJ3In_Open_Ctl",
        "Address": "112",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3#空压机进液阀开启",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40112"
      },
      {
    
        "VarName": "KYJ3In_Close_Ctl",
        "Address": "113",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3#空压机 进液阀关闭",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40113"
      },
      {
    
        "VarName": "KYJ3In_Run_State",
        "Address": "114",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3#空压机进液阀状态",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40114"
      }
    ]
  }
}

版权声明
本文为[罗迪尼亚的熔岩]所创,转载请带上原文链接,感谢
https://blog.csdn.net/helldoger/article/details/122179424

随机推荐