c#中 实现 字符串转字典数据 字符串转字典 数组字典转字符串

  List<Dictionary<string, object>> StringToListForDictionary(string value)
        {
            if (value.Length < 1)
            {
                return null;
            }
            List<Dictionary<string, object>> results = new List<Dictionary<string, object>>();
          //  Console.Write("165value--" + value);
            string[] dicStrs = value.Split(';');
            for(int i = 0; i < dicStrs.Length; i++)
            {
                Dictionary<string, object> dic = StringToDictionary(dicStrs[i]);
                results.Add(dic);
            }
            return results;
        }

        Dictionary<string, object> StringToDictionary(string value)
        {
            if (value.Length < 1)
            {
                return null;
            }
            //Console.Write("1777value--" + value);
            Dictionary<string, object> dic = new Dictionary<string, object>();

            string[] dicStrs = value.Split('|');
            foreach (string str in dicStrs)
            {
            //    Console.Write("183value--" + str);
                string[] strs = str.Split('=');
                dic.Add(strs[0], strs[1]);
            }
            return dic;
        }

        string DictionaryListToString(List<Dictionary<string,object>> listInfo)
        {
            if(listInfo.Count ==0)
            {
                return "";
            }
            string str = "";
            for (int i = 0; i < listInfo.Count; i++)
            {
                Dictionary<string, object> dic = listInfo[i];
              foreach(string key in dic.Keys)
                {
                    str += (key + "=" + dic[key]);
                    str += "|";
                }
                str = str.Substring(0, str.Length - 1);
                str += ";";
            }
            str = str.Substring(0, str.Length - 1);
            return str;
        }
    }

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页