Original address: http://wo13145219.iteye.com/blog/2022667
Using system;using system.collections;using system.configuration;using system.data;using System.Linq;using System.web;using system.web.security;using system.web.ui;using system.web.ui.htmlcontrols;using System.web.ui.webcontrols;using system.web.ui.webcontrols.webparts;using system.xml.linq;using System.IO;using System.text;using system.runtime.serialization.json;using system.collections.generic;using System.Reflection; Using System.web.script.serialization;namespace ajaxtest{public partial class Json:System.Web.UI.Page {PR otected void Page_Load (object sender, EventArgs e) {//response.write (Getjsoninfo ()); String strreg = "\\\\"; Strreg = Strreg.replace ("\\\\", "\ \"); Response.Write (Strreg); Method One Response.Write (Disjsoninfo (Getobjectbyjson (Getjsoninfo ()))); Method Two Response.Write (Jsoninfo.getinfo ()); }///<summary>///Get the entity class to be converted to JSON data for faster delivery on a Web pageData)///</summary>///<returns></returns> public string Getjsoninfo () { UserInfo UserInfo = new UserInfo (); Userinfo.strnameinfo = "Zhang San"; Userinfo.intageinfo = 23; Userinfo.inttelinfo = 66666; Userinfo.straddrinfo = "Beijing"; Userinfo.strpasswordinfo = "yhx.123"; Userinfo.strname = "Zhang San"; Userinfo.intage = 23; USERINFO.STRPSD = "yhx.123"; Userinfo.inttel = 2324; userinfo.straddr = "Beijing"; Serializes the object JSON datacontractjsonserializer serializer = new DataContractJsonSerializer (typeof (UserInfo)); Create a storage area for memory stream System.IO.MemoryStream ms = new MemoryStream (); Writes a JSON string to the memory stream in serializer. WriteObject (MS, UserInfo); System.IO.StreamReader reader = new StreamReader (MS); Ms. Position = 0; String strres = reader. ReadToEnd (); REader. Close (); Ms. Close (); return strres; }///<summary>//Convert JSON data to entity class////</summary>//<returns></return s> private static list<userinfo> Getobjectbyjson (string jsonstring) {//instantiation DataContract The Jsonserializer object, which requires the object type to be serialized datacontractjsonserializer serializer = new DataContractJsonSerializer (typeof (List <UserInfo>)); Save jsonstring = "[" + jsonstring + "]" in the JSON incoming memory stream; MemoryStream stream = new MemoryStream (Encoding.UTF8.GetBytes (jsonstring)); Use the ReadObject method to deserialize object OB = serializer. ReadObject (stream); list<userinfo> ls = (list<userinfo>) ob; return LS; }///<summary>///display the conversion to entity class data on the page///</summary> private string Disjsoninfo (List <UserInfo> us) {string stritem = ""; ForeAch (var item in US) {stritem + = Item.strname + ":" + item.strpsd + ":" + Item.intage + ":" + I Tem.inttel + ":" + item.straddr + "<br/>"; } return stritem; }}///<summary>//User entity class///</summary> public class UserInfo {//user name Publi C string StrName {get; set;} Age public int IntAge {get; set;} Password public string strpsd {get; set;} Phone number public int Inttel {get; set;} Address public string Straddr {get; set;} }///<summary>//Convert JSON data to entity class (method two)///</summary> public static class Jsoninfo {// <summary>///Get the entity class to be converted to JSON data (for faster data delivery on a Web page)///</summary>//<returns></re turns> public static string Getjsoninfo () {UserInfo UserInfo = new UserInfo (); Userinfo.strname = "Zhang San"; Userinfo.intaGE = 23; USERINFO.STRPSD = "yhx.123"; Userinfo.inttel = 2324; userinfo.straddr = "Beijing"; Serializes the object JSON datacontractjsonserializer serializer = new DataContractJsonSerializer (typeof (UserInfo)); Create a storage area for memory stream System.IO.MemoryStream ms = new MemoryStream (); Writes a JSON string to the memory stream in serializer. WriteObject (MS, UserInfo); System.IO.StreamReader reader = new StreamReader (MS); Ms. Position = 0; String strres = reader. ReadToEnd (); Reader. Close (); Ms. Close (); return strres; }//<summary>///</summary>//<returns></returns> Pub Lic static string GetInfo () {String jsonstr = "[" +getjsoninfo () + "]"; List<userinfo> Products; Products = jsoninfo.jsonstringtolist<userinfo> (JSONSTR); String stritem = ""; ForEach (var item in products) {stritem + = Item.strname + ":" + item.strpsd + ":" + Item.intage + ":" + Item.inttel + ":" + item.straddr + "<br/>"; } return stritem; }///<summary>//Returns the List collection object///</summary>//<typeparam name= "T" ></t ypeparam>//<param name= "JSONSTR" ></param>///<returns></returns> Publi C Static List<t> jsonstringtolist<t> (This string jsonstr) {JavaScriptSerializer Serializer = new JavaScriptSerializer (); List<t> Objs = serializer.deserialize<list<t>> (JSONSTR); return OBJS; }//<summary>///</summary>/<typeparam name= "T" ></TYPEPARAM&G T <param name= "JSON" ></param>///<returns></returns> public static T deserialize& Lt T>(String json) {T obj = activator.createinstance<t> (); using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (JSON))) {Datacontractjsonseria Lizer serializer = new DataContractJsonSerializer (obj. GetType ()); Return (T) serializer. ReadObject (MS); } } }}
Go [c#]c# converts JSON into a list entity class and converts the list entity class to a JSON string