C # JSON helper classes (interoperable)

Source: Internet
Author: User

  Public classJsonhelper { PublicJsonhelper () {//            //Todo:add constructor Logic here//        }        /// <summary>        ///serializes an object into a JSON string/// </summary>        /// <typeparam name= "T" >Object Type</typeparam>        /// <param name= "obj" >Object Entity</param>        /// <returns>JSON string</returns>         Public Static stringGetjson<t>(T obj) {//Remember to add references System.ServiceModel.Web            /** * If you do not add the above reference, System.Runtime.Serialization.Json; JSON is not coming out of the OH **/DataContractJsonSerializer JSON=NewDataContractJsonSerializer (typeof(T)); using(MemoryStream ms =NewMemoryStream ()) {JSON.                WriteObject (MS, obj); stringSzjson =Encoding.UTF8.GetString (Ms.                ToArray ()); returnSzjson; }        }        /// <summary>        ///To restore a JSON string to an object/// </summary>        /// <typeparam name= "T" >Object Type</typeparam>        /// <param name= "Szjson" >JSON string</param>        /// <returns>Object Entity</returns>         Public StaticT parseformjson<t> (stringSzjson) {T obj= activator.createinstance<t>(); using(MemoryStream ms =NewMemoryStream (Encoding.UTF8.GetBytes (Szjson))) {DataContractJsonSerializer DCJ=NewDataContractJsonSerializer (typeof(T)); return(T) DCJ.            ReadObject (MS); }        }    }
// because the collection cannot be received in WebService, JSON is selected with the string receive. // Call: List<biz. t_exentrydetailmodel> Exdetailmodel Exdetailmodel = uped. Jsonhelper.parseformjson<list<biz. T_exentrydetailmodel>> (JSONSTR);    // parsing JSON strings as classes   // entity classes convert JSON strings to classes List<Models.ExEntryDetailModel> detailmodel =servers. Getexentrydetailmodel (...); string jsonstr = uped. Jsonhelper.getjson<list<models.exentrydetailmodel>> (Detailmodel);

C # JSON helper classes (interoperable)

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.