Convert dictionary to json string:
public static string ConvertToString<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
IDictionary<string, string> _dic = new Dictionary<string, string>();
foreach (var item in dictionary)
{
if (item.Key.ToString().Contains('.'))
{
string key = item.Key.ToString().Split('.')[1];
_dic.Add(key, item.Value.ToString());
}
else
{
_dic.Add(item.Key.ToString(), item.Value.ToString());
}
}
return string.Join(",", _dic.Select(kv => kv.Key.ToString().Trim() + "*" + kv.Value.ToString().Trim()).ToArray()).Replace(' ', '~').Replace('&', '!');
}
Get json to dictionary:
private void ConvertStringToList(string ColumnList)
{
try
{
if (!string.IsNullOrEmpty(ColumnList))
{
ColumnList = ColumnList.Replace('~', ' ');
ColumnList = ColumnList.Replace("!", "&");
List<string> colList = ColumnList.Split(',').ToList<string>();
foreach (string colItem in colList)
{
if (colItem.Contains('*'))
{
reportColumnList.Add(colItem.Split('*')[0].ToString(), colItem.Split('*')[1].ToString());
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
No comments:
Post a Comment