A Brief analysis of JSON serialization and deserialization

  • 2020-06-19 10:03:28
  • OfStack

Method 1: Introduce the System.Web.Script.Serialization namespace and use the JavaScriptSerializer class to implement a simple serialization class: Personnel


    public class Personnel
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

Perform serialization and deserialization:

protected void Page_Load(object sender, EventArgs e)
        {
            Personnel personnel = new Personnel();
            personnel.Id = 1;
            personnel.Name = " The small white ";
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            // Execute serialization 
            string r1 = jsonSerializer.Serialize(personnel);

            // Perform deserialization 
            Personnel _Personnel = jsonSerializer.Deserialize<Personnel>(r1);
         }

r1 Output: {"Id":1,"Name":" Xiaobai "}
You can use the ScriptIgnore attribute tag to not serialize public properties or public fields.

        public class Personnel
        {
            [ScriptIgnore]
            public int Id { get; set; }
            public string Name { get; set; }
        }

r1 Output: {"Name":" Xiaobai "}

Method 2: introduce System Runtime. Serialization. Json namespace using DataContractJsonSerializer class implements the serialization

Serialization class: People


        public class People
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

Perform serialization and deserialization

protected void Page_Load(object sender, EventArgs e)
        {
            People people = new People();
            people.Id = 1;
            people.Name = " The small white ";

            DataContractJsonSerializer json = new DataContractJsonSerializer(people.GetType());
            string szJson = "";
            // serialization 
            using (MemoryStream stream = new MemoryStream())
            {
                json.WriteObject(stream, people);
                szJson = Encoding.UTF8.GetString(stream.ToArray());
            }
            // deserialization 
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(People));
                People _people = (People)serializer.ReadObject(ms);
            }
         }

szJson Output: {"Id":1,"Name":" Xiaobai "}

You can use IgnoreDataMember: to specify that the member is not part 1 of the data protocol and is not serialized, DataMember: to define serialization property parameters, the DataMember property tag fields must use the DataContract tag class or the DataMember tag will not work.


[DataContract]
        public class People
        {
            [DataMember(Name = "id")]
            public int Id { get; set; }
            [IgnoreDataMember]
            public string Name { get; set; }
        }

Output: {"id":1}


Related articles: