最近做了一个程序, 里面一段代码用到 Json, 数组, Dictionary 转换和数组对比的一些知识, 虽然在实际碰到类似问题时候有更好的方法, 但这就当是一次基础知识的回顾, 现在分享一下.
先介绍下要实现的业务:
有几个批处理程序在运行, 每次运行完写 log 到数据库, 我又写的一个程序每天遍历 log 中显示成功的批处理, 跟需要运行的批处理总数做对比, 如果某个批处理没有成功运行, 就把这个批处理名发短信到我手机.
其中用到了一个 json 的类库, Json.Net, 下载地址 https://www.newtonsoft.com/json, 一个很常用的开源类库, 下载后引用即可;
另外需要进行 json 的序列化和反序列化等操作, 单独使用了一个帮助类, jsonhelper.cs, 代码如下
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using System.IO;
- namespace Alert_Send_Message
- { /// <summary>
- /// Json 帮助类
- /// </summary>
- public class JsonHelper
- {
- /// <summary>
- /// 将对象序列化为 JSON 格式
- /// </summary>
- /// <param name="o">对象</param>
- /// <returns>json 字符串</returns>
- public static string SerializeObject(object o)
- {
- string json = JsonConvert.SerializeObject(o);
- return json;
- }
- /// <summary>
- /// 解析 JSON 字符串生成对象实体
- /// </summary>
- /// <typeparam name="T">对象类型</typeparam>
- /// <param name="json">json 字符串(eg.{"ID":"112","Name":"石子儿"})</param>
- /// <returns > 对象实体</returns>
- public static T DeserializeJsonToObject<T>(string json) where T : class
- {
- JsonSerializer serializer = new JsonSerializer();
- StringReader sr = new StringReader(json);
- object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
- T t = o as T;
- return t;
- }
- /// <summary>
- /// 解析 JSON 数组生成对象实体集合
- /// </summary>
- /// <typeparam name="T">对象类型</typeparam>
- /// <param name="json">json 数组字符串(eg.[{"ID":"112","Name":"石子儿"}])</param>
- /// <returns > 对象实体集合</returns>
- public static List<T> DeserializeJsonToList<T>(string json) where T : class
- {
- JsonSerializer serializer = new JsonSerializer();
- StringReader sr = new StringReader(json);
- object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
- List<T> list = o as List<T>;
- return list;
- }
- /// <summary>
- /// 反序列化 JSON 到给定的匿名对象.
- /// </summary>
- /// <typeparam name="T">匿名对象类型</typeparam>
- /// <param name="json">json 字符串</param>
- /// <param name="anonymousTypeObject">匿名对象</param>
- /// <returns > 匿名对象</returns>
- public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject)
- {
- T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
- return t;
- }
- }
- }
程序主代码:
- <!--app.config-->
- <!-- 短信接收人设置 -->
- <add key="phoneNum" value="123456,345678"/>
- <!-- 所有批处理程序名集合 json 格式(这个程序名因为后期可能会变, 所以写在了 config 里方便配置)-->
- <!-- 当然最好是写在数据库里, 那就没 json 什么事了 -->
- <add key="instanceName" value="[{'LOG_TYPE':'p1','TYPE_NAME':'ws_conon'},{'LOG_TYPE':'p5','TYPE_NAME':'ws_con'}]" />
- /// <summary>
- /// 主程序代码
- /// </summary>
- if (ds.Tables != null && ds.Tables.Count !=0)//ds.Tables 中放入的是成功运行的程序名
- {
- // 从 config 中获取需要序列化的 json 数据, 并转换为字符串
- string str_instance = GetAppConfig("instanceName").ToString().Replace("'","\"");
- // 使用 JsonHelper 类将字符串序列化为 List, 注意要调用下面的转换实体类 < instanceName>
- List<instanceNmae> json_instance = JsonHelper.DeserializeJsonToList<instanceNmae>(str_instance);
- // 将 List 转换为 Dictionary
- Dictionary<string, string> instanceDictionary = json_instance.ToDictionary(key => key.LOG_TYPE, value => value.TYPE_NAME);
- // 将 ds.Tables 中成功运行的程序代号放入数组, 本操作使用 Linq, 需要引用命名空间 using System.Linq
- string[] arrRate = ds.Tables[0].AsEnumerable().Select(d => d.Field<string>("LOG_TYPE")).ToArray();
- // 遍历需要运行的程序总数集合, 将程序代号放入 List
- List<string> strLogTypeList = new List<string>();
- for (int i = 0; i <json_instance.Count; i++)
- {
- strLogTypeList.Add(json_instance[i].LOG_TYPE);
- }
- // 将需要运行的总程序集合 List 转换为数组
- string[] arrType = strLogTypeList.ToArray();// 进程名集合
- // 数组对比
- string[] arrNew = arrType.Except(arrRate).ToArray();// 对比当天没有运行的进程
- if (arrNew.Length != 0)
- {
- string _typeName = "";
- for (int i = 0; i < arrNew.Length; i++)
- {
- string _log_type = arrNew[i];
- _typeName += "[" + instanceDictionary[_log_type] + "]";
- }
- string _log_time = DateTime.Now.AddDays(-1).ToString("yyyyMMdd");// 前一天日期
- // 从 config 中获取接收短信人的电话号码并放入字符串
- string phoneNum = string.Format(GetAppConfig("phoneNum"));
- // 对字符串分列, 放入数组
- string[] phoneArry = phoneNum.Split(new char[] { ',' });
- string content = string.Format("管理员您好,{0}程序 {1} 未运行, 请注意检查!", _typeName, _log_time);
- // 发送短信方法
- send_message(phoneArry, content);
- }
- else {
- string phoneNum = string.Format(GetAppConfig("phoneNum"));
- string[] phoneArry = phoneNum.Split(new char[] { ',' });
- string _log_time = DateTime.Now.AddDays(-1).ToString("yyyyMMdd");// 前一天日期
- string content = string.Format("管理员您好,{0}程序运行正常!", _log_time);
- // 发送短信方法
- send_message(phoneArry, content);
- }
- }
- /// <summary>
- /// Json 序列化实体类
- /// </summary>
- public class instanceNmae
- {
- public string LOG_TYPE { get; set; }
- public string TYPE_NAME { get; set; }
- }
单独说一下数组的数据对比:
- // 实例
- string[] arrA = new string[] { "a", "b", "c", "d" };//A
- string[] arrB = new string[] { "c", "d", "e" };//B
- string[] arrSame = arrA.Intersect(arrB).ToArray();// 相同的数据 (结果: c,d)
- string[] arrAB = arrA.Except(arrB).ToArray();//A 中有 B 中没有的 (结果: a,b)
- string[] arrBA = arrB.Except(arrA).ToArray();//B 中有 A 中没有的 (结果: e)
来源: https://www.cnblogs.com/KyleLi/p/9481920.html