.NET in JSON serialization and deserialization three ways

First popularize Tips:

JSON and JS objects Huzhuan

    To achieve the conversion from JSON string object using the JSON.stringify () Method:

var json = JSON.stringify({a: 'Vinkong', b: 'Sky'}); //结果是 '{"a": "Vinkong", "b": "Sky"}'

    To achieve the conversion from JSON an object using the JSON.parse () Method:

var obj = JSON.parse('{"a": "Vinkong", "b": "Sky"}'); //结果是 {a: 'Vinkong', b: 'Sky'}

  

The following is provided Demo
establish a Student class

 

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace ConsoleApplication1
{
    [DataContract] // the DataContractJsonSerializer way must be added to the contract 
    class Student
    {
        
     
        private string Name;
        [The DataMember] // the DataContractJsonSerializer embodiment must be added contract 
        [ScriptIgnore] // use JavaScriptSerializer embodiment of the tag sequence does not attribute 
        [JsonIgnore] // use JSON.NET embodiment of the tag sequence does not attribute 
        public  String _Name
        {
            get { return Name; }
            set { Name = value; }
        }
        [DataMember]
        private int  age;

        public int _Age
        {
            get { return age; }
            set { age = value; }
        }
        [DataMember]
        private string sex;

        public string _Sex
        {
            get { return sex; }
            set { sex = value; }
        }

    }
}

Write test code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            #region first way: JavaScriptSerializer manner using 
            Student STU = new new Student ();
            stu._Name = "vinkong";
            stu._Age = 15 ;
            stu._Sex = "";
            JsonSerialize JavaScriptSerializer = new new JavaScriptSerializer ();
             // a namespace JavaScriptSerializer embodiment need to be introduced, in the assembly in System.Web.Extensions.dll. 
             // the using System.Web.Script.Serialization;
             // NOTE: Available [ScriptIgnore ] of the tag sequence does not attribute
             // target sequence object 
            String strjson = jsonSerialize.Serialize (STU);
             // Json deserialized 
            STU = jsonSerialize.Deserialize <Student> (strjson);

            Console.WriteLine ( " 1. Use JavaScriptSerializer mode " );
            Console.WriteLine ( " Serialization: " + strjson);
            Console.WriteLine("反序列化:" + stu._Name + "" + " " + "" + stu._Age + "" + " " + " " + stu._Sex + "");
            #endregion

            #region using JSON.NET manner
             // use the namespace library Json.NET Newtonsoft.Json need to introduce the using;
             // NOTE: Available [JsonIgnore] of the tag sequence does not attribute 
            Student STU2 = new new Student ();
            stu2._Name = "vinkong2";
            stu2._Age = 15;
            stu2._Sex = " F " ;
             // Object Serialization Json 
            String strjson2 = JsonConvert.SerializeObject (STU2);
             // Json deserialized 
            STU2 = JsonConvert.DeserializeObject <Student> (strjson2);
            Console.WriteLine ( " 2. Use JSON.NET mode " );
            Console.WriteLine ( " Serialization: " + strjson2);
            Console.WriteLine("反序列化:" + stu2._Name + "" + " " + "" + stu2._Age + "" + " " + " " + stu2._Sex + "");
            #endregion

            #region use DataContractJsonSerializer
              // use DataContractJsonSerializer way need to introduce namespaces, in System.Runtime.Serialization.dll. in.
             // the using System.Runtime.Serialization.Json;
              // entity contract [DataMember], [DataContract], using DataContractJsonSerializer serialization and deserialization must be added, can be added to the other two methods do not add. 
            Stu3 = Student new new Student ();
            stu3._Name = "vinkong3";
            stu3._Age = 22;
            stu3._Sex = "";

            String strjson3 = "" ;
             // object serialization Json 
            the using (= the MemoryStream Stream new new the MemoryStream ())
            {
                DataContractJsonSerializer jsonSerialize2 = new DataContractJsonSerializer(stu3.GetType());
                jsonSerialize2.WriteObject(stream, stu3);
                strjson3 = Encoding.UTF8.GetString(stream.ToArray());
            }
            // Json deserialize objects 
            the using (= the MemoryStream Stream new new the MemoryStream (Encoding.UTF8.GetBytes (strjson3)))
            {
                DataContractJsonSerializer jsonSerialize3 = new DataContractJsonSerializer(typeof(Student));
                stu3 = (Student)jsonSerialize3.ReadObject(stream);
            }
            Console.WriteLine ( " 3. Use DataContractJsonSerializer mode " );
            Console.WriteLine ( " Serialization: " + strjson3);
            Console.WriteLine("反序列化:" + stu3._Name + "" + " " + "" + stu3._Age + "" + " " + " " + stu3._Sex + "");
            Console.ReadKey();
            #endregion

          
        }
    }
}

JSON.NET way to make use of serialization and de-serialization, good performance.

 

Guess you like

Origin www.cnblogs.com/Vinkong/p/12599013.html