当前位置:网站首页>Utilisez le texte. Json analyse le fichier json

Utilisez le texte. Json analyse le fichier json

2022-01-15 02:10:04 La lave de Rodinia

Créer unmodbusBibliothèque de classes variables

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

namespace JsonPractice
{
    
    public class Variable_Modbus
    {
    
        //Nom de la variable
        public string VarName {
     get; set; }
        //Zone de stockage
        public string StoreArea {
     get; set; }
        //Adresse
        public string Address {
     get; set; }
        //Type de données
        public string DataType {
     get; set; }
        //Archivé ou non Enregistrer dans la base de données
        public string IsFiling {
     get; set; }
        //Appelez la police ou non.
        public string IsAlarm {
     get; set; }
        //Faire un rapport ou non
        public string IsReport {
     get; set; }
        //Description du commentaire de la variable
        public string Note {
     get; set; }
        //Adresse absolue
        private string absoluteAddress;
        // Déterminer la zone de stockage 
        public string AbsoluteAddress
        {
    
            get
            {
    
                int store = 0;
                switch (StoreArea)// Déterminer et afficher l'emplacement de stockage à travers la zone de stockage 
                {
    
                    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; //Par défaut
                        break;
                }
                absoluteAddress = (store * 10000 + Convert.ToInt32(Address)).ToString();//30001,40001C'est parti.
                return absoluteAddress;
            }
            set
            {
    
                value = absoluteAddress;
            }
        }
    }
}

Créer unhelper

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
                });
        }
    }
}

Analysejson

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="Zhang San",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();

            /// ConversionXMLPourjson
            //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();

        }
    }
}

jsonDocumentation

{
    
  "Root": {
    
    "Variable": [
      {
    
        "VarName": "LQT_Level",
        "Address": "0",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Niveau de la tour de refroidissement ",
        "IsFiling": "1",
        "IsAlarm": "1",
        "IsReport": "1",
        "AbsoluteAddress": "40000"
      },
      {
    
        "VarName": "LQT_InPre",
        "Address": "2",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Pression d'entrée de la tour de refroidissement ",
        "IsFiling": "1",
        "IsAlarm": "1",
        "IsReport": "1",
        "AbsoluteAddress": "40002"
      },
      {
    
        "VarName": "LQT_InTemp",
        "Address": "4",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Température d'entrée de la tour de refroidissement ",
        "IsFiling": "1",
        "IsAlarm": "1",
        "IsReport": "1",
        "AbsoluteAddress": "40004"
      },
      {
    
        "VarName": "LQT_OutPre",
        "Address": "6",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Pression de sortie de la tour de refroidissement ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40006"
      },
      {
    
        "VarName": "LQT_OutTemp",
        "Address": "8",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Température de sortie de la tour de refroidissement ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40008"
      },
      {
    
        "VarName": "LQT_BSPre",
        "Address": "10",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Pression de maquillage de la tour de refroidissement ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40010"
      },
      {
    
        "VarName": "LQB1_Current",
        "Address": "12",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Courant de la pompe de refroidissement ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40012"
      },
      {
    
        "VarName": "LQB1_Fre",
        "Address": "14",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Fréquence de la pompe de refroidissement ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40014"
      },
      {
    
        "VarName": "LQB2_Current",
        "Address": "16",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# Courant de la pompe de refroidissement ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40016"
      },
      {
    
        "VarName": "LQB2_Fre",
        "Address": "18",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# Fréquence de la pompe de refroidissement ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40018"
      },
      {
    
        "VarName": "KYJ1_OutTemp",
        "Address": "20",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Température de l'eau de retour du compresseur d'air ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40020"
      },
      {
    
        "VarName": "KYJ2_OutTemp",
        "Address": "22",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# Température de l'eau de retour du compresseur d'air ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40022"
      },
      {
    
        "VarName": "KYJ3_OutTemp",
        "Address": "24",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3# Température de l'eau de retour du compresseur d'air ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40024"
      },
      {
    
        "VarName": "CQG1_OutPre",
        "Address": "26",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Pression de sortie du réservoir d'air ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40026"
      },
      {
    
        "VarName": "CQG2_OutPre",
        "Address": "28",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# Pression de sortie du réservoir d'air ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40028"
      },
      {
    
        "VarName": "CQG3_OutPre",
        "Address": "30",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3# Pression de sortie du réservoir d'air ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40030"
      },
      {
    
        "VarName": "Env_Temp",
        "Address": "32",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "Température du point de rosée",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40032"
      },
      {
    
        "VarName": "FQG_Temp",
        "Address": "34",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Température du sous - cylindre ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40034"
      },
      {
    
        "VarName": "FQG_Pre",
        "Address": "36",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Pression du sous - cylindre ",
        "IsFiling": "1",
        "IsAlarm": "0",
        "IsReport": "1",
        "AbsoluteAddress": "40036"
      },
      {
    
        "VarName": "LQT_InPre_H",
        "Address": "38",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Limite supérieure de pression de retour de la tour de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40038"
      },
      {
    
        "VarName": "LQT_InTemp_H",
        "Address": "40",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Limite supérieure de température de l'eau de retour de la tour de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40040"
      },
      {
    
        "VarName": "LQT_OutPre_H",
        "Address": "42",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Limite supérieure de pression d'alimentation en eau de la tour de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40042"
      },
      {
    
        "VarName": "LQT_OutTemp_H",
        "Address": "44",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Température d'alimentation élevée de la tour de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40044"
      },
      {
    
        "VarName": "LQB1_Fre_H",
        "Address": "46",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Haute fréquence de la pompe de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40046"
      },
      {
    
        "VarName": "LQB1_Fre_L",
        "Address": "48",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Basse fréquence de la pompe de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40048"
      },
      {
    
        "VarName": "LQB2_Fre_H",
        "Address": "50",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# Haute fréquence de la pompe de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40050"
      },
      {
    
        "VarName": "LQB2_Fre_L",
        "Address": "52",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# Basse fréquence de la pompe de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40052"
      },
      {
    
        "VarName": "KYJ1_Fre_Set",
        "Address": "54",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Fréquence du compresseur d'air donnée ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40054"
      },
      {
    
        "VarName": "KYJ2_Fre_Set",
        "Address": "56",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# Fréquence du compresseur d'air donnée ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40056"
      },
      {
    
        "VarName": "KYJ3_Fre_Set",
        "Address": "58",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3# Fréquence du compresseur d'air donnée ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40058"
      },
      {
    
        "VarName": "FQG_Temp_H",
        "Address": "60",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Limite supérieure de température du sous - cylindre ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40060"
      },
      {
    
        "VarName": "FQG_Pre_H",
        "Address": "62",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Limite supérieure de pression du sous - cylindre ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40062"
      },
      {
    
        "VarName": "Env_Temp_H",
        "Address": "64",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Limite supérieure de température du point de rosée ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40064"
      },
      {
    
        "VarName": "Env_Temp_L",
        "Address": "66",
        "DataType": "Float",
        "StoreArea": "03 Holding Register(4x)",
        "Note": " Point de rosée limite de température basse ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40066"
      },
      {
    
        "VarName": "LQB1_Start_Ctl",
        "Address": "100",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Démarrage de la pompe de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40100"
      },
      {
    
        "VarName": "LQB1_Stop_Ctl",
        "Address": "101",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Arrêt de la pompe de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40101"
      },
      {
    
        "VarName": "LQB1_Run_State",
        "Address": "102",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# État de fonctionnement de la pompe de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40102"
      },
      {
    
        "VarName": "LQB2_Start_Ctl",
        "Address": "103",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# Démarrage de la pompe de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40103"
      },
      {
    
        "VarName": "LQB2_Stop_Ctl",
        "Address": "104",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2#  Arrêt de la pompe de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40104"
      },
      {
    
        "VarName": "LQB2_Run_State",
        "Address": "105",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# État de fonctionnement de la pompe de refroidissement ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40105"
      },
      {
    
        "VarName": "KYJ1In_Open_Ctl",
        "Address": "106",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Ouverture de la vanne d'entrée du compresseur d'air ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40106"
      },
      {
    
        "VarName": "KYJ1In_Close_Ctl",
        "Address": "107",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# Fermeture de la vanne d'entrée du compresseur d'air ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40107"
      },
      {
    
        "VarName": "KYJ1In_Run_State",
        "Address": "108",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "1# État de la vanne d'entrée du compresseur d'air ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40108"
      },
      {
    
        "VarName": "KYJ2In_Open_Ctl",
        "Address": "109",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# Ouverture de la vanne d'entrée du compresseur d'air ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40109"
      },
      {
    
        "VarName": "KYJ2In_Close_Ctl",
        "Address": "110",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# Fermeture de la vanne d'entrée du compresseur d'air ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40110"
      },
      {
    
        "VarName": "KYJ2In_Run_State",
        "Address": "111",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "2# État de la vanne d'entrée du compresseur d'air ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40111"
      },
      {
    
        "VarName": "KYJ3In_Open_Ctl",
        "Address": "112",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3# Ouverture de la vanne d'entrée du compresseur d'air ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40112"
      },
      {
    
        "VarName": "KYJ3In_Close_Ctl",
        "Address": "113",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3#Compresseur d'air  Vanne d'entrée fermée ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40113"
      },
      {
    
        "VarName": "KYJ3In_Run_State",
        "Address": "114",
        "DataType": "Unsigned",
        "StoreArea": "03 Holding Register(4x)",
        "Note": "3# État de la vanne d'entrée du compresseur d'air ",
        "IsFiling": "0",
        "IsAlarm": "0",
        "IsReport": "0",
        "AbsoluteAddress": "40114"
      }
    ]
  }
}

版权声明
本文为[La lave de Rodinia]所创,转载请带上原文链接,感谢
https://chowdera.com/2022/01/202201080558307246.html

随机推荐