溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

[asp.net]C#實(shí)現(xiàn)json的序列化和反序列化

發(fā)布時(shí)間:2020-06-25 16:53:31 來源:網(wǎng)絡(luò) 閱讀:458 作者:蓬萊仙羽 欄目:編程語言

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

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

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

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)簡(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類實(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);         }       }


輸出的結(jié)果:

[asp.net]C#實(shí)現(xiàn)json的序列化和反序列化


==================== 迂者 丁小未 CSDN博客專欄=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互學(xué)習(xí),共同進(jìn)步 ===================

轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/dingxiaowei2013/article/details/17240085

歡迎關(guān)注我的微博:http://weibo.com/u/2590571922


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI