jsonHelper helper

Prior to use, open source projects need to reference the class using Newtonsoft.Json

Link: https: //pan.baidu.com/s/1QTbTSMNW8jow1YX9WCTblQ
extraction code: 1jli

. 1  the using the System.Collections.Generic;
 2  the using the System.IO;
 . 3  the using Newtonsoft.Json;
 . 4  
. 5  namespace the Common
 . 6  {
 . 7      ///  <Summary> 
. 8      /// Json helper
 9      /// required prior to use open source projects references library: Newtonsoft.Json.dll
 10      ///  </ Summary> 
. 11      public  Sealed  class JsonHelper
 12 is      {
 13 is          ///  <Summary> 
14          /// serialize objects into json format
 15          ///  </ Summary> 
16         ///  <param name = "obj"> sequence object </ param> 
. 17          ///  <Returns> JSON string </ Returns> 
18 is          public  static  String SerializeObjct ( Object obj)
 . 19          {            
 20 is              return JsonConvert.SerializeObject (obj );
 21 is          }
 22 is          ///  <Summary> 
23 is          /// parse JSON string generation target entity
 24          ///  </ Summary> 
25          ///  <typeParam name = "T"> Object type </ typeParam> 
26 is          / //  <param name = "JSON">json string</param>
27         /// <returns></returns>
28         public static T DeserializeJsonToObject<T>(string json) where T:class
29         {
30             JsonSerializer serializer = new JsonSerializer();
31             StringReader sr = new StringReader(json);
32             object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
33             T t = obj as T;
34             return t;
35         }
 36          ///  <Summary> 
37 [          /// parse JSON array to generate a set of entity objects
 38 is          ///  </ Summary> 
39          ///  <typeParam name = "T"> Object Type </ typeParam> 
40          ///  < name = param "JSON"> JSON array </ param> 
41 is          ///  <Returns> entity collection object </ Returns> 
42 is          public  static List <T> DeserializeJsonToList <T> ( String JSON) WHERE T:class
43         {
44             JsonSerializer serializer = new JsonSerializer();
45             StringReader sr = new StringReader(json);
46             object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
47             List<T> list = obj as List<T>;
48             return list;
49         }
50     }
51 }

 

Guess you like

Origin www.cnblogs.com/chenyanbin/p/11184648.html