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); } } }}