public static string ObjToJson<T>(T obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); string retVal = Constant.strNullString; using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, obj); retVal = Encoding.UTF8.GetString(ms.ToArray()); } return retVal; } public static T JsonToObj<T>(string json) { T obj; using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); obj = (T)serializer.ReadObject(ms); } return obj; }
[DataContract] public class MyObject { [DataMember] public int Member1{ get; set; } [DataMember] public string Member2 { get; set; } }