国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#實(shí)現(xiàn)的json序列化和反序列化代碼實(shí)例

2020-01-24 01:47:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
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類(lèi)實(shí)現(xiàn)簡(jiǎn)單的序列化  [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類(lèi)實(shí)現(xiàn)序列化  //可以使用IgnoreDataMember:指定該成員不是數(shù)據(jù)協(xié)定的一部分且沒(méi)有進(jìn)行序列化,DataMember:定義序列化屬性參數(shù),使用DataMember屬性標(biāo)記字段必須使用DataContract標(biāo)記類(lèi) 否則DataMember標(biāo)記不起作用。  [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);    }  }
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 兰考县| 望城县| 广汉市| 东至县| 咸宁市| 凤山县| 思茅市| 黄冈市| 瑞金市| 竹山县| 霞浦县| 闽侯县| 台中县| 湘潭市| 安福县| 镶黄旗| 弥勒县| 板桥市| 汤阴县| 普洱| 道真| 长春市| 云和县| 吴江市| 宁武县| 巴马| 博客| 萨迦县| 抚顺市| 当涂县| 尼玛县| 乌恰县| 牟定县| 西乌珠穆沁旗| 黄龙县| 扎兰屯市| 太和县| 河津市| 西青区| 嘉黎县| 临沂市|