Decode From Json to IDictionary - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Decode From Json to IDictionary

Demo Code


using System.Web;
using Newtonsoft.Json;

public class Main{
        public static void DecodeFromJson(this System.Collections.Generic.IDictionary<string, string> self, string encodedDictionary)
        {/* w  ww. j av  a 2 s .  c om*/
            var dictionary = JsonConvert.DeserializeObject<System.Collections.Generic.Dictionary<string, object>>(encodedDictionary);
            //System.Collections.Generic.Dictionary<string, object> dictionary = JsonConvert.DeserializeObject(encodedDictionary) as System.Collections.Generic.Dictionary<string, object>;
            if (dictionary == null)
            {
                throw new System.ArgumentException("Invalid request format.", "encodedDictionary");
            }
            foreach (System.Collections.Generic.KeyValuePair<string, object> current in dictionary)
            {
                if (current.Value == null)
                {
                    self.Add(current.Key, null);
                }
                else if (current.Value is object[])
                {
                    self.Add(current.Key, JsonConvert.SerializeObject(current.Value));
                }
                else
                {
                    self.Add(current.Key, current.Value.ToString());
                }
            }
        }
}

Related Tutorials