C implements the serialization and deserialization instance code of json

  • 2020-05-26 10:00:28
  • OfStack

When asp.net and unity communicate with http, when the unity client sends out the form request, I will return the data he wants to request to the client in the form of json for the client to parse. This one piece on the server side is concerned with the serialization and deserialization of json.

There are examples of both approaches. The first is to use a generic collection to store objects and then serialize the collection for 1. The second is a direct serialization of an object


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
{
    // methods 1 Introduced: System.Web.Script.Serialization Namespace usage  JavaScriptSerializer Class implements simple serialization 
    [Serializable]
    public class Person
    {

        private int id;
        /// <summary>
        /// id
        /// </summary>
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;
        /// <summary>
        ///  The name 
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
    // methods 2 Introduced:  System.Runtime.Serialization.Json Namespace usage  DataContractJsonSerializer Class implementation serialization 
    // You can use IgnoreDataMember: Specifies that this member is not a data protocol 1 Partial and not serialized, DataMember: Define the serialization property parameter to use DataMember Property tag fields must be used DataContract Tag class   Otherwise, DataMember The tag doesn't work. 
    [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 serialization 
            string s = js.Serialize(listperson);
            Response.Write(s);
 
            // methods 2
            Person1 p11 = new Person1();
            p11.Id = 1;
            p11.Name = "hello";
            p11.Sex = " male ";
            DataContractJsonSerializer json = new DataContractJsonSerializer(p11.GetType());
            string szJson = "";
            // serialization 
            using (MemoryStream stream = new MemoryStream())
            {
                json.WriteObject(stream, p11);
                szJson = Encoding.UTF8.GetString(stream.ToArray());
                Response.Write(szJson);
            }
            // deserialization 
            //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);
        }

    }


Related articles: