乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       昵稱10504424 2013-12-12

      在做asp.net和unity進(jìn)行http通信的時候,當(dāng)unity客戶端發(fā)出表單請求的時候,我要將他要請求的數(shù)據(jù)以json的格式返回給客戶端,讓客戶端來解析。服務(wù)器端這一塊就涉及到j(luò)son的序列化和反序列化的問題。

      接下來就來舉個列子,當(dāng)然包括兩種方法(本篇參考自:http://www./csharpspace/10822r2908.shtml)

      兩種方法都有例子,第一種是用泛型集合來存儲的對象,然后將集合序列化一下;第二種是直接序列化的一個對象

      復(fù)制代碼
      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類實(shí)現(xià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類實(shí)現(xiàn)序列化
          //可以使用IgnoreDataMember:指定該成員不是數(shù)據(jù)協(xié)定的一部分且沒有進(jìn)行序列化,DataMember:定義序列化屬性參數(shù),使用DataMember屬性標(biāo)記字段必須使用DataContract標(biāo)記類 否則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ù)制代碼

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多