引入 System.Runtime.Serialization.Json命名空间使用 DataContractJsonSerializer类实现序列化
序列化类:People
执行序列化反序列化public class People
{
public int Id { get ; set ; }
public string Name { get ; set ; }
}
protected void Page_Load( object sender, EventArgs e)
{
People people = new People();
people.Id = 1 ;
people.Name = " 小白 " ;
DataContractJsonSerializer json = new DataContractJsonSerializer(people.GetType());
string szJson = "" ;
// 序列化
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, people);
szJson = Encoding.UTF8.GetString(stream.ToArray());
}
// 反序列化
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer( typeof (People));
People _people = (People)serializer.ReadObject(ms);
}
}
szJson输出结果:{"Id":1,"Name":"小白"}
可以使用IgnoreDataMember:指定该成员不是数据协定的一部分且没有进行序列化,DataMember:定义序列化属性参数,使用DataMember属性标记字段必须使用DataContract标记类
否则DataMember标记不起作用。
[DataContract]
public class People
{
[DataMember(Name = " id " )]
public int Id { get ; set ; }
[IgnoreDataMember]
public string Name { get ; set ; }
}
输出结果: {"id":1}
进行简单封装一下
using System.Text;
using System.Runtime.Serialization.Json;
using System.IO;
namespace RRQ.Common
{
public static class JsonExtension
{
/// <summary>
/// 序列化Json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="target"></param>
/// <returns></returns>
public static string JsonSerialize<T>( this object target)
{
T result = (T)target;
DataContractJsonSerializer json = new DataContractJsonSerializer(result.GetType());
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, result);
return Encoding.UTF8.GetString(stream.ToArray());
}
}
/// <summary>
/// 返序列化Json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="target"></param>
/// <returns></returns>
public static T JsonDeserialize<T>( this string target)
{
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(target)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer( typeof(T));
return (T)serializer.ReadObject(ms);
}
}
}
}
using System.Runtime.Serialization.Json;
using System.IO;
namespace RRQ.Common
{
public static class JsonExtension
{
/// <summary>
/// 序列化Json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="target"></param>
/// <returns></returns>
public static string JsonSerialize<T>( this object target)
{
T result = (T)target;
DataContractJsonSerializer json = new DataContractJsonSerializer(result.GetType());
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, result);
return Encoding.UTF8.GetString(stream.ToArray());
}
}
/// <summary>
/// 返序列化Json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="target"></param>
/// <returns></returns>
public static T JsonDeserialize<T>( this string target)
{
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(target)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer( typeof(T));
return (T)serializer.ReadObject(ms);
}
}
}
}