hch
8 天以前 cb653cf75b17b9bdca0b5b9e6b88edf1ca72e90c
Main/Config/ConfigParse.cs
@@ -62,6 +62,7 @@
        return string.Empty;
    }
    //一维数组:来源格式如 1|2|3|4
    public static T[] GetMultipleStr<T>(string msg) where T : struct
    {
        string[] segs = GetMultipleStr(msg);
@@ -105,6 +106,8 @@
        return msg.Split('_');
    }
    //一维数组:来源格式如 1_2|2_3|3_1|4_3;
    //返回一维数组结构如 1|2|3|4
    public static T[] GetKeyValueKeys<T>(string msg) where T : struct
    {
        string[] segs = GetMultipleStr(msg);
@@ -124,6 +127,8 @@
        return null;
    }
    //一维数组:来源格式如 1_2|2_3|3_1|4_3;
    //返回一维数组结构如 2|3|1|3
    public static T[] GetKeyValueValues<T>(string msg) where T : struct
    {
        string[] segs = GetMultipleStr(msg);
@@ -143,6 +148,31 @@
        return null;
    }
    //二维数组:来源格式如 1_2_1|2_3_4|3_1_2|4_3_4
    public static T[][] GetArray2<T>(string msg) where T : struct
    {
        string[] segs = GetMultipleStr(msg);
        if (segs != null && segs.Length > 0)
        {
            T[][] array = new T[segs.Length][];
            for (int i = 0; i < segs.Length; i++)
            {
                string[] pair = GetKeyValue(segs[i]);
                if (pair.Length > 1)
                {
                    array[i] = new T[pair.Length];
                    for (int j = 0; j < pair.Length; j++)
                    {
                        array[i][j] = (T)Convert.ChangeType(pair[j], typeof(T));
                    }
                }
            }
            return array;
        }
        return null;
    }
    //字典:来源格式如 1_2|2_3|3_1|4_3;
    public static Dictionary<T, P> GetDic<T, P>(string msg)
    {
        Dictionary<T, P> dic = null;
@@ -177,7 +207,6 @@
    }
    //{'17':['63','6','27'],'65':['800'],'55':['139'],'19':['1000','2600','130']}
    public static Regex userDataRegex = new Regex(@"'([0-9]+)':\[(.*?)\]", RegexOptions.Singleline);
    public static Dictionary<int, List<int>> Analysis(string val)//正则表达式的字符串分割
    {
        string s = ServerStringTrim(val);
@@ -187,50 +216,57 @@
        }
        s = s.Replace(" ", string.Empty);
        if (!userDataRegex.IsMatch(s))
        {
            return null;
        }
        else
        {
            Dictionary<int, List<int>> dics = new Dictionary<int, List<int>>();
            foreach (Match match in userDataRegex.Matches(s))
            {
                int id = int.Parse(match.Groups[1].Value);
                string str = match.Groups[2].Value;
                string[] vals = str.Split(',');
                List<int> list = new List<int>();
                for (int i = 0; i < vals.Length; i++)
                {
                    int intval = int.Parse(vals[i].Replace('\'', ' '));
                    list.Add(intval);
                }
                if (!dics.ContainsKey(id))
                {
                    dics.Add(id, list);
                }
            }
            return dics;
        }
    }
    public static Dictionary<int, List<int>> ParseJsonDict(string jsonStr)
    {
        if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr))
        {
            return new Dictionary<int, List<int>>();
        }
        var dict = JsonMapper.ToObject<Dictionary<string, List<int>>>(jsonStr);
        var dict = JsonMapper.ToObject<Dictionary<string, string[]>>(s);
        if (dict == null || dict.Count == 0)
            return null;
        Dictionary<int, List<int>> result = new Dictionary<int, List<int>>();
        foreach (var item in dict)
        {
            result[int.Parse(item.Key)] = item.Value;
            List<int> list = new List<int>();
            for (int i = 0; i < item.Value.Length; i++)
            {
                list.Add(int.Parse(item.Value[i]));
            }
            if (list.Count != 0)
                result[int.Parse(item.Key)] = list;
        }
        return result;
        //if (!userDataRegex.IsMatch(s))
        //{
        //    return null;
        //}
        //else
        //{
        //    Dictionary<int, List<int>> dics = new Dictionary<int, List<int>>();
        //    foreach (Match match in userDataRegex.Matches(s))
        //    {
        //        int id = int.Parse(match.Groups[1].Value);
        //        string str = match.Groups[2].Value;
        //        string[] vals = str.Split(',');
        //        List<int> list = new List<int>();
        //        for (int i = 0; i < vals.Length; i++)
        //        {
        //            int intval = int.Parse(vals[i].Replace('\'', ' '));
        //            list.Add(intval);
        //        }
        //        if (!dics.ContainsKey(id))
        //        {
        //            dics.Add(id, list);
        //        }
        //    }
        //    return dics;
        //}
    }
    //json格式: {"1":1}
    public static Dictionary<int, int> ParseIntDict(string jsonStr)
    {
        if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr))
@@ -248,6 +284,7 @@
        return result;
    }
    //json格式: {"1":[1,2],"2":[3,4]}
    public static Dictionary<int, int[]> ParseIntArrayDict(string jsonStr)
    {
        if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr))
@@ -265,6 +302,7 @@
        return result;
    }
    //json格式: {"1":[[1,2],[3,4]]}
    public static Dictionary<int, int[][]> ParseIntArray2Dict(string jsonStr)
    {
        if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr))