C # Use the json interface,

Source: Internet
Author: User

C # Use the json interface,

A few days ago, the project was changed from using the database to using the interface. Because some additional information exists in the xml returned by the interface, the converted xml able cannot be stored in MemCache. At this time, we can serialize xml into its corresponding class. Of course, because there was not much serialization use of xml at the time, we changed it to receive json and convert it into the corresponding class. Here we mainly show how to use json.

After receiving the passed json string, use JsonConvert. DeserializeObject <T> (string value); to convert it to the corresponding type. So I wrote the first type to be converted, as shown below:

/// <Summary> /// used to serialize A json string /// </summary> public class JsonSet {public string message {set; get;} public string code {set; get;} public DataTable entitylist {set; get ;}}

Then transform the interface to call the intermediate function

      private static bool GetJsonSearchData<T>(string param, string url, string method, int length, ref T jsonResult, string encodeWay = "gb2312")        {            bool isSucc = 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;                }            }            return isSucc;        }

The usage is as follows:

1   JsonSet jset = new JsonSet();2             SearchInterface.PostSearchData<JsonSet>(parms, url, ref jset);3             if (jset != null && jset.entitylist != null)4             {5                 return jset.entitylist;6             }

The returned DataTable format is a standard format because it fully fits the class created in the json character format, solving the problem of storage in Memcache. Later, it was found that the DataTable needs to be converted into the corresponding object. It is not necessary to save the trouble to directly convert the json into the corresponding object. However, if all the classes used in the project need to be modified, it is too much and inconvenient. Later, we found that the json string has only two more attributes: code and message, so we transformed the object into the following:

1   public  class JsonTList<T>2     {3         public string code { set; get; }4         public string message { set; get; }5         public List<T> entitylist { set; get; }6     }

For example

1    JsonTList<AgtInfoEntity> jT = new JsonTList<AgtInfoEntity>();2                 SearchInterface.PostSearchData<JsonTList<AgtInfoEntity>>(parms, url, ref jT);3                 if (jT != null && jT.entitylist != null && jT.entitylist.Count > 0)4                 {5                     agtInfoEntity = jT.entitylist;6                     CacheManager.Set(cacheName, agtInfoEntity);7                 }

This forms our new interface system.

 

Related Article

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.