Thursday, September 26, 2013

Pass and get json string from page to page in Silverlight

Convert dictionary to json string:
 public static string ConvertToString<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
        {            
            IDictionary<stringstring> _dic = new Dictionary<stringstring>();
            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