出现序列化类型对象循环引用
这里是因为序列化类型比较复杂,直接用JavaScriptSerializer将其序列化不成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class PagedService : IHttpHandler { public void ProcessRequest (HttpContext context) { context .Response.ContentType = "text/plain" ; //context.Response.Write("Hello World"); string action = context.Request [ "action" ]; if (action == "getpagecount" ) { var adapter = new T_CommentsTableAdapter(); int count = adapter.SelectCount ().Value; int pagecount = count / 10; if (count %10!= 0) { pagecount ++; } context .Response.Write(pagecount ); } else if (action== "getpagedata" ) { string pagenum = context.Request [ "pagenum" ]; int iPageNum = Convert.ToInt32 (pagenum); var adapter = new T_CommentsTableAdapter(); var data = adapter.GetPageData((iPageNum - 1 ) * 10+1 ,iPageNum*10); //得到所在页的评论 JavaScriptSerializer jss = new JavaScriptSerializer(); context .Response.Write(jss .Serialize(data)); //这里不能成功,因为序列化类型比较复杂 } } |
处理方法
添加一个comment类
进行简单化处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //新建Comment类的List List<Comment > list = new List<Comment >(); foreach ( var row in data ) { list.Add ( new Comment () { PostDate = row .PostDate, Msg = row .Msg }); } JavaScriptSerializer jss= new JavaScriptSerializer(); context.Response .Write( jss.Serialize (list)); //转化成简单的Comment对象以后再进行序列化 public class Comment { public DateTime PostDate { get ; set ; } public string Msg { get ; set ; } } |