出现序列化类型对象循环引用

这里是因为序列化类型比较复杂,直接用JavaScriptSerializer将其序列化不成功

567993-20160316215426256-1688237267.png

567993-20160316215426631-1154337776.png
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类
进行简单化处理
567993-20160316215426928-2096458821.png
567993-20160316215427131-512577253.png

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  ; }
}
567993-20160316215427599-1225912259.png

转载于:https://www.cnblogs.com/zhxshseu/p/5285375.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值