protected void Page_Load(object sender, EventArgs e)
{
Personnel personnel = new Personnel();
personnel.Id = 1;
personnel.Name = "小白";
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
//執(zhí)行序列化
string r1 = jsonSerializer.Serialize(personnel);
//執(zhí)行反序列化
Personnel _Personnel = jsonSerializer.DeserializePersonnel>(r1);
}
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);
}
}
可以使用IgnoreDataMember:指定該成員不是數(shù)據(jù)協(xié)定的一部分且沒有進(jìn)行序列化,DataMember:定義序列化屬性參數(shù),使用DataMember屬性標(biāo)記字段必須使用DataContract標(biāo)記類 否則DataMember標(biāo)記不起作用。