惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
腾讯CDC
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
博客园 - Franky
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
B
Blog RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Simon Willison's Weblog
Simon Willison's Weblog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
S
Schneier on Security
MyScale Blog
MyScale Blog
The Last Watchdog
The Last Watchdog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Troy Hunt's Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
K
Kaspersky official blog
F
Fortinet All Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
Security Latest
Security Latest
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
B
Blog
有赞技术团队
有赞技术团队
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Latest news
Latest news
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Help Net Security
Help Net Security
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
J
Java Code Geeks

博客园 - 名刘天下

转载:[UML]Free UML Tools 转载:HTTP协议漫谈 面向对象分析与设计资料收集 Fiddler本机调试的方法 【推荐】可以搜索下载国外pdf,word和ppt资料的好地方 关于管理科学与工程 发现一个好的网站! “关于挨踢(通“IT”)的事儿” 实现SqlServer2005数据库的定时备份 链接远程数据库时,提示“”provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错“解决方案! 什么是效用计算? 关于云计算 Gartner发布2010四大趋势 【转载】白话面向智能体编程 服务计算 什么是元数据? 关于科研的几篇文章不错,记录一下? [转自网络]《十年只为一个摧残的梦》 JSON资料收集
序列化类型为“****”的对象时检测到循环引用。
名刘天下 · 2011-10-18 · via 博客园 - 名刘天下

序列化类型为“****”的对象时检测到循环引用。

2011-10-18 09:44  名刘天下  阅读(2362)  评论()    收藏  举报

问题:

2个类:MetaSet和MetaObject,为1:N的关系。

在Asp.net mvc2.0下,返回Json数据时,出现“序列化类型为“DataCenter.Core.Domain.MetaSet”的对象时检测到循环引用。”

Mvc代码如下:

  IList<MetaSet> sets = new List<MetaSet>();
  sets = _metaSetRepository.GetMetaSetByParentTreeCodeID(treeCodeID, CheckState.yes);
     if (Request.IsAjaxRequest())
        {
               return new JsonResult
                {
                    Data =sets,
                    JsonRequestBehavior= JsonRequestBehavior.AllowGet
                };
  }

数据操作代码如下:

  public IList<MetaSet> GetMetaSetByParentTreeCodeID(string treeCodeID, CheckState state)
        {
            
            var query = Session.CreateQuery(@" select m from MetaSet m where m.TreeCode like :treecodeid and m.IsDelete=False and m.CheckState=:checkState order by m.TreeCode asc")
                       .SetString("treecodeid", treeCodeID + "%").SetEnum("checkState", state);
            return query.List<MetaSet>();
         

        }

原因:

  在MVC里面返回一个json对象的数据,在这个过程中将我们找到的这条数据进行序列化为json对象的一个过程。在这个过程的时候,由于这个对象有映射 关系,那么它在序列化MetaSet对象的时候会序列化该对象的属性MetaObject对象,而这个属性MetaObject对象又有属性MetaSet对象,依次反复。就导致了这个问题的产生。

解决方案:

规避json序列化的时候直接序列化该MetaSet对象,改为序列化不带这种映射关系的对象;

        public IList<MetaSet> GetMetaSetByParentTreeCodeID2(string treeCodeID, CheckState state)
{

var query = Session.CreateQuery(@" select new MetaSet(m.MetaSetID,m.Name,m.ParentID,m.ControllerAction) from MetaSet m where m.TreeCode like :treecodeid and m.IsDelete=False and m.CheckState=:checkState order by m.TreeCode asc")
.SetString("treecodeid", treeCodeID + "%").SetEnum("checkState", state);
return query.List<MetaSet>();

}

参考:

http://archive.cnblogs.com/a/1956117/