您的位置:首页 > Web前端 > JavaScript

.Net 2.0自带的Json序列化、反序列化方法

2015-10-21 14:45 621 查看
public class JsonUtil
{
public static T DeserializeObject<T>(string json)
{
DataContractJsonSerializer jsonFormator = new DataContractJsonSerializer(typeof(T));
using (Stream readStream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
return (T)jsonFormator.ReadObject(readStream);
}
}

public static string SerializeObject(object obj)
{
DataContractJsonSerializer jsonFormator = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream stream = new MemoryStream())
{
jsonFormator.WriteObject(stream, obj);
return Encoding.UTF8.GetString(stream.ToArray());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: