Implementation class of JavaScriptSerializer

Source: Internet
Author: User
Recently began to write their own projects, and finally summon up the courage to take the leap that I think!

First of all, the general helper class to share with you, the first is the object into a JSON sequence of classes, this online there are many, but I have to practice the most can not be used, or have a variety of bugs, in fact, there is a good class in C # can solve the problem, He is the--javascriptserializer class, with this class, just a few simple lines of code, you can convert your object type to JSON forward output!

First we have to refer to the System.Web.Extensions DLL in the project before we can use the JavaScriptSerializer class

The next is the code, very simple.


using system;using system.collections.generic;using System.Linq;using        System.web;using system.web.script.serialization;using system.data;namespace codehelper{public class JsonHelper { <summary>///object to JSON///</summary>//<param name= "obj" > Object </para             m>//<returns>json format string </returns> public static string Objecttojson (Object obj) {            JavaScriptSerializer JSS = new JavaScriptSerializer (); try {return JSS.            Serialize (obj); } catch (Exception ex) {throw new Exception ("Jsonhelper.objecttojson ():" + ex.            Message); }        }    }}

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.