C# 将MSMQ消息转换成Json格式

PS:主要就是一个配置文件和一个转换函数


配置文件app.config  之前要ADD reference -->   system.configuration & using.system.configuration

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings>
    <add key="Lable.Port1" value="MessageID"/>
    <add key="Lable.Port2" value="SendingBaseTag"/>
    <add key="Lable.Port3" value="SendingMachineName"/>
    <add key="Lable.Port4" value="MessageQueuePathName"/>
    <add key="Lable.Port5" value="CustomField1"/>
    <add key="Lable.Port6" value="CustomField2"/>

    <add key="Body.Port1" value="DataMode"/>
    <add key="Body.Port2" value="DataModeID"/>
    <add key="Body.Port3" value="ExperimentID"/>
    <add key="Body.Port4" value="BasePenType"/>
    <add key="Body.Port5" value="BasePenTypeID"/>
    <add key="Body.Port6" value="MID"/>
    <add key="Body.Port7" value="StationTag"/>
    <add key="Body.Port8" value="DateTime"/>
    <add key="Body.Port9" value="Path1"/>
    <add key="Body.Port10" value="EquipmentID1"/>
    <add key="Body.Port11" value="Path2"/>
    <add key="Body.Port12" value="EquipmentID2"/>
  </appSettings>

</configuration>

转换函数:

    private string ConvertToJSON(string label, string body)
        {
            //TODO: convert to json
            string[] Lablelist = label.Split('|');
            string[] Bodylist = body.Split('|');
            
            string[] LableName = new string[6];
            string[] BodyName = new string[12];
            
            string JsonStr = "{\"";
        
            for(int j=1;j<7;j++)
            {
                 
                 JsonStr  = JsonStr + ConfigurationManager.AppSettings["Lable.Port" + j.ToString()] +"\":\""+Lablelist[j-1] + "\",\"";
                 
            }
            for (int i = 1; i < 13;i++ )
            {
                JsonStr = JsonStr + ConfigurationManager.AppSettings["Body.Port" + i.ToString()] + "\":\"" + Bodylist[i - 1] + "\",\"";
            }

            JsonStr = JsonStr.Remove(JsonStr.Length - 2, 2)+"}";

        }
因为不是计算机专业,研究了下 花了5个小时
是不是很简单?
(*^__^*) 嘻嘻

C# 将MSMQ消息转换成Json格式

上一篇:Windows下安装并设置Redis


下一篇:利用SpringBoot+RabbitMQ,实现一个邮件推送服务(三)