原文地址: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 { protected void Page_Load(object sender, EventArgs e) { //Response.Write(getJsonInfo()); //string strReg = "\\\\"; //strReg = strReg.Replace("\\\\", "\\"); ////Response.Write(strReg); //方法一 Response.Write(disJsonInfo(getObjectByJson(getJsonInfo()))); //方法二 Response.Write(JsonInfo.getInfo()); } /// <summary> /// 获取将实体类转换为json数据(目的是为了更快在网页上传递数据) /// </summary> /// <returns></returns> public string getJsonInfo() { UserInfo userInfo = new UserInfo(); //userInfo.strNameInfo = "张三"; //userInfo.intAgeInfo = 23; //userInfo.intTelInfo = 66666; //userInfo.strAddrInfo = "北京市"; //userInfo.strPasswordInfo = "yhx.123"; userInfo.strName = "张三"; userInfo.intAge = 23; userInfo.strPsd = "yhx.123"; userInfo.intTel = 2324; userInfo.strAddr = "北京市"; //将对象序列化json DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UserInfo)); //创建存储区为内存流 System.IO.MemoryStream ms = new MemoryStream(); //将json字符串写入内存流中 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> /// 将json数据转换成实体类 /// </summary> /// <returns></returns> private static List<UserInfo> getObjectByJson(string jsonString) { // 实例化DataContractJsonSerializer对象,需要待序列化的对象类型 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<UserInfo>)); //把Json传入内存流中保存 jsonString = "[" + jsonString + "]"; MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); // 使用ReadObject方法反序列化成对象 object ob = serializer.ReadObject(stream); List<UserInfo> ls = (List<UserInfo>)ob; return ls; } /// <summary> /// 在页面上显示转换为实体类数据 /// </summary> private string disJsonInfo(List<UserInfo> us) { string strItem = ""; foreach(var item in us ) { strItem += item.strName + ":" + item.strPsd + ":" + item.intAge + ":" + item.intTel + ":" + item.strAddr + "<br/>"; } return strItem; } } /// <summary> /// 用户实体类 /// </summary> public class UserInfo { //用户名 public string strName { get; set; } //年龄 public int intAge { get; set; } //密码 public string strPsd { get; set; } //电话号码 public int intTel { get; set; } //地址 public string strAddr { get; set; } } /// <summary> /// 将json数据转换成实体类(方法二) /// </summary> public static class JsonInfo { /// <summary> /// 获取将实体类转换为json数据(目的是为了更快在网页上传递数据) /// </summary> /// <returns></returns> public static string getJsonInfo() { UserInfo userInfo = new UserInfo(); userInfo.strName = "张三"; userInfo.intAge = 23; userInfo.strPsd = "yhx.123"; userInfo.intTel = 2324; userInfo.strAddr = "北京市"; //将对象序列化json DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UserInfo)); //创建存储区为内存流 System.IO.MemoryStream ms = new MemoryStream(); //将json字符串写入内存流中 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> public 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> /// 返回List集合对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="JsonStr"></param> /// <returns></returns> public 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> /// <param name="json"></param> /// <returns></returns> public static T Deserialize<T>(string json) { T obj = Activator.CreateInstance<T>(); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); return (T)serializer.ReadObject(ms); } } } }