方法一
public class Personnel
{
public int Id { get; set; }
public string Name { get; set; }
}
執行序列化反序列化
protected void Page_Load(object sender
{
Personnel personnel = new Personnel();
personnel
personnel
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
//執行序列化
string r
//執行反序列化
Personnel _Personnel = jsonSerializer
}
r
可以使用 ScriptIgnore屬性標記不序列化公共屬性或公共字段
public class Personnel
{
[ScriptIgnore]
public int Id { get; set; }
public string Name { get; set; }
}
r
方法二
序列化類
public class People
{
public int Id { get; set; }
public string Name { get; set; }
}
執行序列化反序列化
protected void Page_Load(object sender
{
People people = new People();
people
people
DataContractJsonSerializer json = new DataContractJsonSerializer(people
string szJson = "";
//序列化
using (MemoryStream stream = new MemoryStream())
{
json
szJson = Encoding
}
//反序列化
using (MemoryStream ms = new MemoryStream(Encoding
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));
People _people = (People)serializer
}
}
szJson輸出結果
可以使用IgnoreDataMember:指定該成員不是數據協定的一部分且沒有進行序列化
[DataContract]
public class People
{
[DataMember(Name = "id")]
public int Id { get; set; }
[IgnoreDataMember]
public string Name { get; set; }
}
輸出結果: {"id":
From:http://tw.wingwit.com/Article/program/net/201311/14251.html