C # using the JSON interface

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.