In C # Json serialized

main idea:

  Using nuget download "Newtonsoft.Json" package, in which the method may call json of various objects. Deserialization call "JsonConvert.DeserializeObject <DataTable> ()" method.

  Example:

string str_json;
            List <the Person> PS;
             // initialize the database object 
            var D1 = new new myDatabase ();
             // initialize a class object ordinary 
            var P1 = new new the Person () = {Xm of " Zhang strong " , Nl = 20 is };
             // initialize a DataTable object 
            DataTable dt = new new DataTable ();
            dt.Columns.Add("Age", Type.GetType("System.Int32"));
            dt.Columns.Add("Name", Type.GetType("System.String"));
            dt.Columns.Add("Sex", Type.GetType("System.String"));
            dt.Columns.Add("IsMarry", Type.GetType("System.Boolean"));
            for (int i = 0; i < 4; i++)
            {
                DataRow dr = dt.NewRow();
                dr["Age"] = i + 1;
                dr["Name"] = "Name" + i;
                dr["Sex"] = i % 2 == 0 ? "" : "";
                dr["IsMarry"] = i % 2 > 0 ? true : false;
                dt.Rows.Add(dr);
            }
            // each sequence of output 
            str_json = JsonConvert.SerializeObject (d1.t1);
            Console.WriteLine(str_json);
            Console.WriteLine();
            // just deserialize a 
            PS JsonConvert.DeserializeObject = <List <>> the Person (str_json);
            str_json = JsonConvert.SerializeObject(p1);
            Console.WriteLine(str_json);
            Console.WriteLine();
            str_json = JsonConvert.SerializeObject(dt);
            Console.WriteLine(str_json);
            Console.WriteLine();
            // output deserialization thing 
            foreach ( var Item in PS)
            {
                Console.WriteLine($"{item.Xm}\t{item.Nl}");
            }
            Console.ReadKey();

 

Guess you like

Origin www.cnblogs.com/wanjinliu/p/11703109.html