using System.Runtime.Serialization.Json;
public class infoObject
{
public String name { get; set; }
public String birthday { get; set; }
public String id { get; set; }
public String email { get; set; }
public infoObject()
{
name = "";
birthday = "";
id = "";
email = "";
}
public infoObject(String newname, String newbirthday, String newid, String newemail)
{
name = newname;
birthday = newbirthday;
id = newid;
email = newemail;
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
infoObject info = new infoObject("peng","19920412","342423188209121234","
[email protected]");
string szJson;
//å°å¯¹è±¡è½¬æå符串
DataContractJsonSerializer json = new DataContractJsonSerializer(info.GetType());
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, info);//æ对象填å
å°æµé(å
³é®ï¼
szJson = Encoding.UTF8.GetString(stream.ToArray());//ææå®æ ¼å¼ææµç¼ç æå符串
Response.Write( "对象转æå符串:<br>"+szJson+"<br>");
}
//å°å符串转æ对象
//infoObject obj = Activator.CreateInstance<infoObject>();
infoObject obj = new infoObject();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj=(infoObject)serializer.ReadObject(ms);//å°å«æå符串çæµè½¬æ对象
}
Response.Write("å符串转对象:"+obj.name + "<br>" + obj.birthday + "<br>" + obj.id + "<br>" + obj.email);
}
}