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

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());
            }
        }
    }

上一篇:ny20 吝啬的国度


下一篇:(转)C#调用C函数(DLL)传递参数问题