您的位置:首页 > 编程语言 > C#

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

2017-07-06 12:16 826 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐