问题描述
我试图序列被自动从实体数据模型的.edmx生成的POCO类,当我用
I tried to serialize POCO class that was automatically generated from Entity Data Model .edmx and when I used
JsonConvert.SerializeObject
我得到了以下错误:
I got the following error:
类型System.Data.Entity的检测错误自参考循环。
我该如何解决这个问题?
How do I solve this problem?
推荐答案
使用JsonSerializerSettings
-
ReferenceLoopHandling.Error
(默认值)会报错。的这就是为什么你会得到一个异常。的 -
ReferenceLoopHandling.Serialize
如果对象嵌套,但非常有用不下去。 -
ReferenceLoopHandling.Ignore
将不序列化一个对象。
如果遇到一个参考循环
如果是自身的子对象
ReferenceLoopHandling.Error
(default) will error if a reference loop is encountered. This is why you get an exception.ReferenceLoopHandling.Serialize
is useful if objects are nested but not indefinitely.ReferenceLoopHandling.Ignore
will not serialize an object if it is a child object of itself.
例如:
JsonConvert.SerializeObject(YourPOCOHere, Formatting.Indented,
new JsonSerializerSettings {
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
});
你应该有序列化,无限嵌套你可以使用一个对象<一个href=\"http://james.newtonking.com/json/help/index.html?topic=html/$p$pserveObjectReferences.htm\">$p$pserveObjectReferences避免StackOverflowException
Should you have to serialize an object that is nested indefinitely you can use PreserveObjectReferences to avoid a StackOverflowException.
例如:
JsonConvert.SerializeObject(YourPOCOHere, Formatting.Indented,
new JsonSerializerSettings {
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
挑选什么使你感觉被序列化对象。
Pick what makes sense for the object you are serializing.
http://james.newtonking.com/json/help/
这篇关于检测类型JSON.NET错误自参考循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!