// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.IO; using System.Web.Script.Serialization; namespace E { public static class Json { public static object Deserialize(string json) { JavaScriptSerializer ser = new JavaScriptSerializer(); return ser.DeserializeObject(json); } private static void Dump(IDictionary o, TextWriter tw, string ind) { foreach(KeyValuePair kvp in o) { tw.Write(ind + kvp.Key + ": "); if(kvp.Value is string) { tw.WriteLine((string)kvp.Value); } else if (kvp.Value is IDictionary) { tw.WriteLine(); Dump((IDictionary)kvp.Value, tw, ind + " "); } else { throw new Exception(kvp.Value.GetType().FullName + " not supported yet"); } } } public static void Dump(IDictionary o, TextWriter tw) { Dump(o, tw, ""); } } public class Program { public static void Main(string[] args) { string json = "{\"a\":\"This is a\",\"b\":{\"c\":\"This is c\",\"d\":\"This is d\"}}"; Console.WriteLine(json); IDictionary o = (IDictionary)Json.Deserialize(json); Json.Dump(o, Console.Out); } } }