C# uses System.Runtime.Serialization.Json to serialize and deserialize

basic concepts:

(1) Serialization refers to the process of converting an object into a byte sequence.
Deserialization refers to the process of restoring a byte sequence to an object.
(2) The most important role of serialization: to ensure the integrity and transferability of the object when transferring and saving the object. The object is converted into an ordered byte stream for transmission on the network or stored in a local file.
The most important role of deserialization: according to the object state and description information stored in the byte stream, the object is reconstructed through deserialization.
Summary: The core role is the preservation and reconstruction of the object state. (The core point of the whole process is the object state and description information stored in the byte stream)

Json

(1) What is Json
Json [javascript object representation method], it is a lightweight data exchange format, we can easily read and write it, and it is easy to be transformed and generated by the computer, it is completely Independent of language.

Json supports the following two data structures:

A collection of key-value pairs-a variety of different programming languages ​​support this data structure;

An ordered collection of list type values-this includes arrays, collections, vectors, or sequences, etc.

Json has the following manifestations

The object is
an unordered "key/value", an object starts with curly braces "{" and ends with curly braces "}". After each "key", there is a colon, and a comma is used to separate multiple Key-value pairs. E.g:

var user = {
    
    "name":"Manas","gender":"Male","birthday":"1987-8-8"}  

Array

Set the order of values. An array starts with square brackets "[" and ends with square brackets "]", and all values ​​are separated by commas, for example:

var userlist = [{
    
    "user":{
    
    "name":"Manas","gender":"Male","birthday":"1987-8-8"}},

{
    
    "user":{
    
    "name":"Mohapatra","Male":"Female","birthday":"1987-7-7"}}]

String

Any number of Unicode characters are marked with quotation marks and separated by backslashes. E.g:

var userlist = "{\"ID\":1,\"Name\":\"Manas\",\"Address\":\"India\"}"

Use Runtime.Serialization.Json to serialize into Json and deserialize into objects

  1. Create an entity for transmission or storage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
 
namespace JsonSerializerAndDeSerializer
{
    
    
    [DataContract]   //契约,序列化的标识,是使用DataContractJsonSerializer序列化和反序列化必须要加的
   public class Student
    {
    
    
        [DataMember]   //契约,序列化的标识,是使用DataContractJsonSerializer序列化和反序列化必须要加的
       public int ID {
    
     get; set; }
 
        [DataMember]
       public string Name {
    
     get; set; }
 
        [DataMember]
       public int Age {
    
     get; set; }
 
        [DataMember]
       public string Sex {
    
     get; set; }
    }
}

2. Program code
reference assembly and namespace

using System.Runtime.Serialization.Json;
 Student stu = new Student()
             {
    
    
                 ID = 01,
                 Name = "钉钉",
                 Sex = "男",
                 Age = 1
             };
            //序列化
            DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Student));
            MemoryStream msObj = new MemoryStream();
            //将序列化之后的Json格式数据写入流中
            js.WriteObject(msObj, stu);
            msObj.Position = 0;
            //从0这个位置开始读取流中的数据
            StreamReader sr = new StreamReader(msObj, Encoding.UTF8);
            string json = sr.ReadToEnd();
            sr.Close();
            msObj.Close();
 
 
            //反序列化
            string toDes = json;
            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(toDes)))
            {
    
    
                DataContractJsonSerializer deseralizer = new DataContractJsonSerializer(typeof(Student));
                Student model = (Student)deseralizer.ReadObject(ms);// //反序列化ReadObject

            }

Guess you like

Origin blog.csdn.net/sunzheng176/article/details/113249751