zoukankan      html  css  js  c++  java
  • 出现序列化类型对象循环引用

    这里是因为序列化类型比较复杂,直接用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 { getset; }    
    public string Msg { getset ; }
    }

  • 相关阅读:
    kali 2019 安装
    centos 7 安装
    windows 和linux系统下常用命令
    JavaScript中Object类型的定义
    《WPF编程宝典(第2版)》第3章 布局
    《C语言程序设计:现代方法(第2版)》第7章 基本类型
    《C语言程序设计:现代方法(第2版)》第6章 循环
    《C语言程序设计:现代方法(第2版)》第5章 选择语句
    《C语言程序设计:现代方法(第2版)》第4章 表达式
    《深入实践Spring Boot》第4章 提高数据库访问性能
  • 原文地址:https://www.cnblogs.com/zhxshseu/p/5285375.html
Copyright © 2011-2022 走看看