using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Extensions; using System.Web.Script.Serialization; using System.IO; using System.Runtime.Serialization.Json; using System.Runtime.Serialization; namespace ReservationCarTool { public static class JsonToClass { public static String ModelToJson(this Object o) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); String objJson = Serializer.Serialize(o); return objJson; } public static List<T> JSONStringToList<T>(this string JsonStr) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); List<T> objs = Serializer.Deserialize<List<T>>(JsonStr); return objs; } public static T JSONStringToModel<T>(this string JsonStr) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); T objs = Serializer.Deserialize<T>(JsonStr); return objs; } public static T Deserialize<T>(string json) { T obj = Activator.CreateInstance<T>(); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); return (T)serializer.ReadObject(ms); } } } }