Implementation of JavaScriptSerializer-common JsonHelper classes and javascriptserializer
I recently started to write my own projects, and finally took the courage to take this big step I think!
Let's share with you the common helper class. The first class is the class for converting objects into json sequences. There are many online classes, but most of them are unavailable, or there are various bugs. In fact, there is a good class in C # that can solve this problem. It is the -- JavaScriptSerializer class. With this class, you only need a few lines of code, you can convert your object type into json and output it to the foreground!
First, we need to reference the dll System. Web. Extensions in the project to use the JavaScriptSerializer class.
The next step is the code, which is 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> // convert the object to JSON /// </summary> /// <param name = "obj"> Object </ param> // <returns> JSON 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 );}}}}