序列化和反序列化

c#序列化和反序列化问题:

public static class JSON
{
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonString"></param>
/// <returns></returns>
public static T parse<T>(string jsonString)
{
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
}
}

/// <summary>
/// 序列化
/// </summary>
/// <param name="jsonObject"></param>
/// <returns></returns>
public static string stringify(object jsonObject)
{
using (var ms = new MemoryStream())
{
new DataContractJsonSerializer(jsonObject.GetType()).WriteObject(ms, jsonObject);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}

posted @ 2015-11-24 08:54  明&天  阅读(145)  评论(0编辑  收藏  举报