Csharp/C#教程:C#实现json的序列化和反序列化实例代码分享

在做asp.net和unity进行http通信的时候,当unity客户端发出表单请求的时候,我要将他要请求的数据以json的格式返回给客户端,让客户端来解析。服务器端这一块就涉及到json的序列化和反序列化的问题。

两种方法都有例子,第一种是用泛型集合来存储的对象,然后将集合序列化一下;第二种是直接序列化的一个对象
代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Web.Script.Serialization;
usingSystem.Configuration;
usingSystem.Runtime.Serialization.Json;
usingSystem.Runtime.Serialization;
usingSystem.IO;
usingSystem.Text;

namespaceWebApplication1
{

   //方法一:引入System.Web.Script.Serialization命名空间使用JavaScriptSerializer类实现简单的序列化
   [Serializable]
   publicclassPerson
   {
       privateintid;
       ///<summary>
       ///id
       ///</summary>
       publicintId
       {
           get{returnid;}
           set{id=value;}
       }

       privatestringname;
       ///<summary>
       ///姓名
       ///</summary>
       publicstringName
       {
           get{returnname;}
           set{name=value;}
       }
   }

   //方法二:引入System.Runtime.Serialization.Json命名空间使用DataContractJsonSerializer类实现序列化
   //可以使用IgnoreDataMember:指定该成员不是数据协定的一部分且没有进行序列化,DataMember:定义序列化属性参数,使用DataMember属性标记字段必须使用DataContract标记类否则DataMember标记不起作用。
   [DataContract]
   publicclassPerson1
   {
       [IgnoreDataMember]
       publicintId{get;set;}

       [DataMember(Name=”name”)]
       publicstringName{get;set;}
       [DataMember(Name=”sex”)]
       publicstringSex{get;set;}

   }

   publicpartialclass_Default:System.Web.UI.Page
   {
       stringconstr=ConfigurationManager.ConnectionStrings[“connstr”].ConnectionString;
       protectedvoidPage_Load(objectsender,EventArgse)
       {
           Personp1=newPerson();
           p1.Id=1;
           p1.Name=”dxw”;
           Personp2=newPerson();
           p2.Id=2;
           p2.Name=”wn”;

           List<Person>listperson=newList<Person>();
           listperson.Add(p1);
           listperson.Add(p2);

           JavaScriptSerializerjs=newJavaScriptSerializer();
           //json序列化
           strings=js.Serialize(listperson);
           Response.Write(s);

 

           //方法二
           Person1p11=newPerson1();
           p11.Id=1;
           p11.Name=”hello”;
           p11.Sex=”男”;
           DataContractJsonSerializerjson=newDataContractJsonSerializer(p11.GetType());

           stringszJson=””;

           //序列化

           using(MemoryStreamstream=newMemoryStream())

           {

               json.WriteObject(stream,p11);

               szJson=Encoding.UTF8.GetString(stream.ToArray());

               Response.Write(szJson);
           }

           //反序列化

           //using(MemoryStreamms=newMemoryStream(Encoding.UTF8.GetBytes(szJson)))

           //{

           //   DataContractJsonSerializerserializer=newDataContractJsonSerializer(typeof(People));

           //   Person1_people=(Person1)serializer.ReadObject(ms);

           //}
        }
       

       protectedvoidButton1_Click(objectsender,EventArgse)
       {
           Response.Write(constr);
       }

   }

您可能感兴趣的文章:C#中Json序列化去掉null值的方法C#实现JSON字符串序列化与反序列化的方法C#中实现Json序列化与反序列化的几种方式C#实现的json序列化和反序列化代码实例C#实现简单的JSON序列化功能代码实例C#实体对象序列化成Json并让字段的首字母小写的两种解决方法

标签: js 序列化 json

C#字符串内存分配与驻留池学习分享

c++隐式类型转换示例分享

上述就是C#学习教程:C#实现json的序列化和反序列化实例代码分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/905101.html

(0)
上一篇 2021年10月22日
下一篇 2021年10月22日

精彩推荐