温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

[asp.net]C#实现json的序列化和反序列化

发布时间:2020-06-25 16:53:31 来源:网络 阅读:448 作者:蓬莱仙羽 栏目:编程语言

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

接下来就来举个列子,当然包括两种方法(本篇参考自:http://www.csharpwin.com/csharpspace/10822r2908.shtml)

两种方法都有例子,第一种是用泛型集合来存储的对象,然后将集合序列化一下;第二种是直接序列化的一个对象

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);         }       }


输出的结果:

[asp.net]C#实现json的序列化和反序列化


==================== 迂者 丁小未 CSDN博客专栏=================

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

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互学习,共同进步 ===================

转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/17240085

欢迎关注我的微博:http://weibo.com/u/2590571922


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI