本文翻译自:JSON.NET Error Self referencing loop detected for type

I tried to serialize POCO class that was automatically generated from Entity Data Model .edmx and when I used 我尝试序列化从实体数据模型.edmx和使用时自动生成的POCO类

JsonConvert.SerializeObject

I got the following error: 我收到以下错误:

Error Self referencing loop detected for type System.data.entity occurs . 错误检测到类型为System.data.entity的自引用循环。

How do I solve this problem? 我该如何解决这个问题?


#1楼

参考:https://stackoom.com/question/V2Ln/检测到JSON-NET错误类型的自引用循环


#2楼

That was the best solution https://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7 那是最好的解决方案https://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7

Fix 1: Ignoring circular reference globally 修复1:全局忽略循环引用

(I have chosen/tried this one, as have many others) (我选择/尝试了这个,还有很多其他选择)

The json.net serializer has an option to ignore circular references. json.net序列化程序可以忽略循环引用。 Put the following code in WebApiConfig.cs file: 将以下代码放入WebApiConfig.cs文件中:

 config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;

The simple fix will make serializer to ignore the reference which will cause a loop. 简单的修复将使序列化程序忽略引用,这将导致循环。 However, it has limitations: 但是,它有局限性:

  • The data loses the looping reference information 数据丢失循环参考信息
  • The fix only applies to JSON.net 该修复仅适用于JSON.net
  • The level of references can't be controlled if there is a deep reference chain 如果引用链很深,则无法控制引用级别

If you want to use this fix in a non-api ASP.NET project, you can add the above line to Global.asax.cs , but first add: 如果要在非API ASP.NET项目中使用此修复程序,可以将上述行添加到Global.asax.cs ,但首先添加:

var config = GlobalConfiguration.Configuration;

If you want to use this in .Net Core project, you can change Startup.cs as: 如果要在.Net Core项目中使用此功能,可以将Startup.cs更改为:

  var mvc = services.AddMvc(options =>{...}).AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

Fix 2: Preserving circular reference globally 修复2:全局保留循环引用

This second fix is similar to the first. 此第二个修复程序类似于第一个。 Just change the code to: 只需将代码更改为:

config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;

The data shape will be changed after applying this setting. 应用此设置后,数据形状将更改。

[{"$id":"1","Category":{"$id":"2","Products":[{"$id":"3","Category":{"$ref":"2"},"Id":2,"Name":"Yogurt"},{"$ref":"1"}],"Id":1,"Name":"Diary"},"Id":1,"Name":"Whole Milk"},{"$ref":"3"}
]

The $id and $ref keeps the all the references and makes the object graph level flat, but the client code needs to know the shape change to consume the data and it only applies to JSON.NET serializer as well. $ id和$ ref保持所有引用并使对象图级别平坦,但是客户端代码需要知道形状变化以使用数据,并且它仅适用于JSON.NET序列化器。

Fix 3: Ignore and preserve reference attributes 修复3:忽略并保留引用属性

This fix is decorate attributes on model class to control the serialization behavior on model or property level. 此修复程序修饰模型类上的属性,以控制模型或属性级别上的序列化行为。 To ignore the property: 要忽略该属性:

 public class Category { public int Id { get; set; } public string Name { get; set; } [JsonIgnore] [IgnoreDataMember] public virtual ICollection<Product> Products { get; set; } }

JsonIgnore is for JSON.NET and IgnoreDataMember is for XmlDCSerializer. JsonIgnore用于JSON.NET,而IgnoreDataMember用于XmlDCSerializer。 To preserve reference: 要保留参考:

 // Fix 3 [JsonObject(IsReference = true)] public class Category { public int Id { get; set; } public string Name { get; set; } // Fix 3 //[JsonIgnore] //[IgnoreDataMember] public virtual ICollection<Product> Products { get; set; } } [DataContract(IsReference = true)] public class Product { [Key] public int Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public virtual Category Category { get; set; } }

JsonObject(IsReference = true)] is for JSON.NET and [DataContract(IsReference = true)] is for XmlDCSerializer. JsonObject(IsReference = true)]用于JSON.NET, [DataContract(IsReference = true)]用于XmlDCSerializer。 Note that: after applying DataContract on class, you need to add DataMember to properties that you want to serialize. 请注意:在对类应用DataContract之后,需要将DataMember添加到要序列化的属性。

The attributes can be applied on both json and xml serializer and gives more controls on model class. 这些属性可以同时应用于json和xml序列化程序,并提供了对模型类的更多控制。


#3楼

The fix is to ignore loop references and not to serialize them. 解决方法是忽略循环引用,而不是序列化它们。 This behaviour is specified in JsonSerializerSettings . 此行为在JsonSerializerSettings指定。

Single JsonConvert with an overload: 具有重载的单个JsonConvert

JsonConvert.SerializeObject(YourObject, Formatting.Indented,new JsonSerializerSettings() {ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore}
);

Global Setting with code in Application_Start() in Global.asax.cs: 使用Global.asax.cs中Application_Start()中的代码进行全局设置 :

JsonConvert.DefaultSettings = () => new JsonSerializerSettings {Formatting = Newtonsoft.Json.Formatting.Indented,ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
};

Reference: https://github.com/JamesNK/Newtonsoft.Json/issues/78 参考: https : //github.com/JamesNK/Newtonsoft.Json/issues/78


#4楼

You can apply an attribute to the property too. 您也可以将属性应用于属性。 The [JsonProperty( ReferenceLoopHandling = ... )] attribute is well suited to this. [JsonProperty( ReferenceLoopHandling = ... )]属性非常适合于此。

For example: 例如:

/// <summary>
/// Represents the exception information of an event
/// </summary>
public class ExceptionInfo
{// ...code omitted for brevity.../// <summary>/// An inner (nested) error./// </summary>[JsonProperty( ReferenceLoopHandling = ReferenceLoopHandling.Ignore, IsReference = true )]public ExceptionInfo Inner { get; set; }// ...code omitted for brevity...
}

Hope that helps, Jaans 希望有帮助,Jaans


#5楼

The simplest way to do this is to install Json.NET from nuget and add the [JsonIgnore] attribute to the virtual property in the class, for example: 最简单的方法是从nuget安装Json.NET ,并将[JsonIgnore]属性添加到类中的virtual属性,例如:

    public string Name { get; set; }public string Description { get; set; }public Nullable<int> Project_ID { get; set; }[JsonIgnore]public virtual Project Project { get; set; }

Although these days, I create a model with only the properties I want passed through so it's lighter, doesn't include unwanted collections, and I don't lose my changes when I rebuild the generated files... 尽管这些天来,我创建的模型只包含我想传递的属性,因此它比较轻巧,不包含不需要的集合,并且在重建生成的文件时不会丢失更改...


#6楼

To ignore loop references and not to serialize them globally in MVC 6 use the following in startup.cs: 要忽略循环引用而不在MVC 6中全局对其进行序列化,请在startup.cs中使用以下内容:

    public void ConfigureServices(IServiceCollection services){services.AddMvc().Configure<MvcOptions>(options =>{options.OutputFormatters.RemoveTypesOf<JsonOutputFormatter>();var jsonOutputFormatter = new JsonOutputFormatter();jsonOutputFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;options.OutputFormatters.Insert(0, jsonOutputFormatter);});}

检测到JSON.NET错误类型的自引用循环相关推荐

  1. 错误类型、混淆矩阵及目标检测常用评价指标

    目标检测常用评价指标 本文主要参考陈恺大佬在B站商汤账号的介绍mmdetection的视频. 检测结果的正确/错误类型 真阳性(Ture Positive):算法检测到了某类物体(Positive), ...

  2. php json语法错误,在PHP json_decode()中检测到错误的json数据?

    在PHP json_decode()中检测到错误的json数据? 通过json_decode()解析时,我正在尝试处理错误的json数据. 我正在使用以下脚本: if(!json_decode($_P ...

  3. HTTP 响应行 错误类型响应码

    新增网关响应(知识补充,和文章内容无关) 网关响应 网关响应指未能成功处理API请求,从而产生的错误响应. API网关提供默认的网关响应(default).如果您需要自定义响应状态码或网关响应内容,可 ...

  4. java程序错误类型及异常处理

    本文转载至:http://www.cnblogs.com/liaoliao/ 一.程序的错误类型 在程序设计中,无论规模是大是小,错误总是难免的.程序的设计很少有能够一次完成,没有错误的(不是指Hel ...

  5. 【程序设计】程序错误类型

    程序错误 如果程序在测试运行时遇到问题,我们必须调试,也就是说必须定位并排除错误,难度取决于错误的类型和程序员的调试技巧. 程序可能出现的两类基本错误类型是语法错误和逻辑错误. 语法错误 语法错误是指 ...

  6. json和jsonb类型——PostgreSQL

    PostgreSQL支持两种json数据类型:json和jsonb,而两者唯一的区别在于效率,json是对输入的完整拷贝,使用时再去解析,所以它会保留输入的空格,重复键以及顺序等.而jsonb是解析输 ...

  7. Oracle所有错误类型

    ORA-00001: 违反唯一约束条件 (.)  ORA-00017: 请求会话以设置跟踪事件  ORA-00018: 超出最大会话数  ORA-00019: 超出最大会话许可数  ORA-00020 ...

  8. python语法错误类型_python常见报错类型和异常处理

    更新ing 常见的报错类型和简析.异常处理.其他类型的报错(异常)类型简介.自定义异常 常见的报错类型和简析: 报错类型 报错内容 错误类型判断 错误解决方式 AttributeError 属性错误: ...

  9. 程序错误类型及其处理

    程序在设计调试甚至运行都难免出现错误,我们要做的是检测错误. 程序库错误 指的是程序库实现错误,当然,程序库的提供者在程序库发布之前,肯定想尽可能多地检测和纠正错误,但是任何比较大的程序库在发布的时候 ...

最新文章

  1. python适合做后端开发吗-用Python做后台开发,看这一篇就够了
  2. Python 套接字-判断socket服务端有没有关闭的方法实例演示,查看socket运行状态
  3. ubuntu apt-get 默认下载路径
  4. 来自未来团队伙伴的一封信
  5. 《计算机程序设计艺术》pdf
  6. opensource项目_来自Opensource.com的开放硬件资源
  7. .sln文件和.suo文件的解释
  8. BigDecimal 加减乘除及对比
  9. WIN32 汇编 工具栏的使用
  10. c语言万能头文件用不,万能头文件不能用?
  11. MATLAB2015a中Simulink使用S函数的方法全过程
  12. 怎样正确做 Web 应用的压力测试?
  13. jQuery插件库超级好用库
  14. codelite开发php,wxWidgets(2):一个好用C/C++ php 开源IDE -- CodeLite IDE
  15. shell脚本之国际象棋棋盘
  16. PAT_乙级_1011_筱筱
  17. 动力学矩阵法计算石墨烯声子谱
  18. SM2 SM3 SM4加密java实现
  19. 调试串口导致烧录失败
  20. Moodle官方主题文档中文版(自用)

热门文章

  1. linux杂谈(十七):iscsi存储分离技术
  2. Silverlight/Windows8/WPF/WP7/HTML5周学习导读(8月13日-8月19日)
  3. 网工路由交换相关配置
  4. WPF下字体模糊的问题
  5. 设计模式笔记之六:生产消费者模式
  6. python-appium手机自动化测试(仅需安装包)前期准备(pydev-eclipse编辑器)
  7. java生成缩略图,旋转,水印,截图
  8. ZOJ 1094 带括号的矩阵连乘
  9. Python办公自动化(六)|自动更新表格,
  10. zabbix监控操纵系统日志