C#實現的json序列化和還原序列化代碼執行個體

來源:互聯網
上載者:User
using System;using System.Collections.Generic;using System.Web.Script.Serialization;using System.Configuration;using System.Runtime.Serialization.Json;using System.Runtime.Serialization;using System.IO;using System.Text;  namespace WebApplication1{   //方法一:引入System.Web.Script.Serialization命名空間使用 JavaScriptSerializer類實現簡單的序列化  [Serializable]  public class Person  {         private int id;    /// <summary>    /// id    /// </summary>    public int Id    {      get { return id; }      set { id = value; }    }     private string name;    /// <summary>    /// 姓名    /// </summary>    public string Name    {      get { return name; }      set { name = value; }    }  }   //方法二:引入 System.Runtime.Serialization.Json命名空間使用 DataContractJsonSerializer類實現序列化  //可以使用IgnoreDataMember:指定該成員不是資料合約的一部分且沒有進行序列化,DataMember:定義序列化屬性參數,使用DataMember屬性標記欄位必須使用DataContract標記類 否則DataMember標記不起作用。  [DataContract]  public class Person1  {         [IgnoreDataMember]    public int Id { get; set; }     [DataMember(Name = "name")]    public string Name { get; set; }    [DataMember(Name = "sex")]    public string Sex { get; set; }   }   public partial class _Default : System.Web.UI.Page  {    string constr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;         protected void Page_Load(object sender, EventArgs e)    {             Person p1 = new Person();      p1.Id = 1;      p1.Name = "dxw";      Person p2 = new Person();      p2.Id = 2;      p2.Name = "wn";       List<Person> listperson = new List<Person>();      listperson.Add(p1);      listperson.Add(p2);       JavaScriptSerializer js = new JavaScriptSerializer();      //json序列化      string s = js.Serialize(listperson);      Response.Write(s);         //方法二      Person1 p11 = new Person1();      p11.Id = 1;      p11.Name = "hello";      p11.Sex = "男";      DataContractJsonSerializer json = new DataContractJsonSerializer(p11.GetType());       string szJson = "";       //序列化       using (MemoryStream stream = new MemoryStream())       {         json.WriteObject(stream, p11);         szJson = Encoding.UTF8.GetString(stream.ToArray());         Response.Write(szJson);      }       //還原序列化       //using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))       //{       //  DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));       //  Person1 _people = (Person1)serializer.ReadObject(ms);       //}     }           protected void Button1_Click(object sender, EventArgs e)    {      Response.Write(constr);    }    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.