Some days ago, the project was modified from the use of the database to use the interface, because the interface returned by some additional information in the XML, resulting in XML converted DataTable can not be stored in memcache. XML can then be serialized to its corresponding class, of course, because the serialization of XML was not used so much, so instead of receiving JSON converted to the corresponding class, here mainly shows how I use JSON.
After receiving the passed JSON string, use Jsonconvert.deserializeobject<t> (string value) and convert to the appropriate type. So I wrote down the first type that needed to be transformed, as follows:
<summary>///// for serializing JSON strings/// </summary> public class Jsonset {public string Message{set;get;} public string Code {set; get;} Public DataTable entitylist {set; get;} }
And then transform the interface call the middle function
Private Static BOOLGetjsonsearchdata<t> (stringParamstringUrlstringMethodintLengthrefT Jsonresult,stringEncodeway ="gb2312") { BOOLISSUCC =true; StringBuilder SB=getsearchdata (param, url, method, length, encodeway); if(SB! =NULL&& sb. Length >0) { Try{Jsonresult= jsonconvert.deserializeobject<t>(sb.) ToString ()); } Catch{ISSUCC=false; } } returnISSUCC; }
Use methods such as:
1 New Jsonset (); 2 ref jset); 3 if NULL NULL )4 {5 return jset.entitylist; 6 }
The returned DataTable format is a standard format that solves the problem of storing in memcache because of classes created entirely in JSON character format. Later found in the use of the original need to convert the datatable to the corresponding object, if the direct conversion of JSON to the corresponding object, it is not necessary to save something. However, if the class used in the project changes too much, it is inconvenient, later found that the JSON string relative to these classes only the code, message two properties, so the object is transformed into the following
1 Public classJsontlist<t>2 {3 Public stringCode {Set;Get; }4 Public stringMessage {Set;Get; }5 PublicList<t> Entitylist {Set;Get; }6}
Use the same way as
1Jsontlist<agtinfoentity> JT =NewJsontlist<agtinfoentity>();2Searchinterface.postsearchdata<jsontlist<agtinfoentity>> (parms, URL,refJT);3 if(JT! =NULL&& Jt.entitylist! =NULL&& JT.entitylist.Count >0)4 {5Agtinfoentity =jt.entitylist;6 Cachemanager.set (CacheName, agtinfoentity);7}
The result is our new interface system.
C # using the JSON interface