ASP.NET中JSON的序列化和反序列化
sp; 1: Person p = new Person();
2: p.Name = "张三"; 3: p.Age = 28; 4: p.LastLoginTime = DateTime.Now; 5: 6: string jsonString = JsonHelper.JsonSerializer<Person>(p); 序列化结果: {"Age":28,"LastLoginTime":"\/Date(1294499956278+0800)\/","Name":"张三"} 1. 在后台使用正则表达式对其替换处理。修改JsonHelper: 1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Web; 5: using System.Runtime.Serialization.Json; 6: using System.IO; 7: using System.Text; 8: using System.Text.RegularExpressions; 9: 10: /// <summary> 11: /// JSON序列化和反序列化辅助类 12: /// </summary> 13: public class JsonHelper 14: { 15: /// <summary> 16: /// JSON序列化 17: /// </summary> 18: public static string JsonSerializer<T>(T t) 19: { 20: DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); 21: MemoryStream ms = new MemoryStream(); 22: ser.WriteObject(ms, t); 23: string jsonString = Encoding.UTF8.GetString(ms.ToArray()); 24: ms.Close(); 25: //替换Json的Date字符串 26: string p = @"\\/Date\((\d+)\+\d+\)\\/"; 27: MatchEvalu |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |