Note the main points.
The reference that was added when 1.jsonhelp was written. System.Runtime.Serialization.Json;
2. Entity classes must be declared public
Jsonhelp Code:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using system.runtime.serialization.json;using System.io;namespace jsontest{class Jsonhelp {public JsonHelp ( ) {////Todo:add constructor logic here//}///<summary>//Serializing the object JSON string </summary>//<typeparam name= "T" > Object type </typeparam>//<param name= "obj" > object entity </PA ram>//<returns>json strings </returns> public static string getjson<t> (T obj) {//Remember to add a citation With System.ServiceModel.Web/** * If not add the above reference, System.Runtime.Serialization.Json; JSON is not out of the OH * * */DataContractJsonSerializer JSON = new DataContractJsonSerializer (typeof (T)); using (MemoryStream ms = new MemoryStream ()) {json. WriteObject (MS, obj); String Szjson = Encoding.UTF8.GetString (ms. ToArray ()); return Szjson; } }///<summary>///To restore the JSON string to an object///</summary>//<typeparam name= "T" > Object type </typepa ram>//<param name= "Szjson" >json strings </param>///<returns> Object entities </returns> public Stati C T parseformjson<t> (string szjson) {T obj = activator.createinstance<t> (); using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (Szjson))) {DataContractJsonSerializer DCJ = new DataContractJsonSerializer (typeof (T)); Return (T) DCJ. ReadObject (MS); } } }}
Entity Class Code
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; namespace jsontest{ public class TestData {public testData () { } public int Id {get; set ; Public string Name {get; set;} public string Sex {get; set;}} }
Console Application test Code
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace jsontest{class Program {static void Main (string[] args) {//entity class to JSON TestData T1 = new TestData (); T1. Id = 1; T1. Name = "001 name"; T1. Sex = "male"; TestData t2 = new TestData (); T2. Id = 2; T2. Name = "002 Name"; T2. Sex = "male"; testData t3 = new TestData (); T3. Id = 3; T3. Name = "003 Name"; T3. Sex = "male"; list<testdata> tlist = new list<testdata> (); Tlist. ADD (t1); Tlist. ADD (T2); Tlist. ADD (T3); Console.WriteLine (jsonhelp.getjson<list<testdata>> (tlist)); Console.readkey (); JSON to entity class list<testdata> TL = Jsonhelp.parseformjson <List<testData>> (jsonhelp.getjson<list& Lt;testdata>> (tlist)); Console.WriteLine (TL. Count); Console.WriteLine (Tl[0]. Name); Console.readkey (); } }}
In the process of finding information, reference to the classmate's blog
Http://www.cnblogs.com/jinho/archive/2010/04/18/1715044.html
http://blog.csdn.net/wang_cel/article/details/44587201
. NET entity classes and JSON transformations (. NET self-bringing class library implementations)