public static T JSONToObject<T>(string json)
{
T obj
= Activator.CreateInstance<T>();
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
DataContractJsonSerializer serializer
= new DataContractJsonSerializer(obj.GetType());
return (T)serializer.ReadObject(stream);
}
}

public static string ObjectToJSON<T> (T t)
{
DataContractJsonSerializer serializer
= new DataContractJsonSerializer(t.GetType());
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, t);
return Encoding.UTF8.GetString(stream.ToArray());
}
}
posted on 2011-12-03 23:20  kitea  阅读(107)  评论(0)    收藏  举报