EF+NewtonSoft报循环引用错
JsonSerializerSettings setting = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.Objects//,
//NullValueHandling = NullValueHandling.Ignore
};
PBInfo p = new PBInfoBLL().Get(5000);
string s = JsonConvert.SerializeObject(p, Formatting.None, setting);
上面的代码运行到JsonConvert.SerializeObject就死住了,再也不动了,好象一直在序列化循环对象,
如果只是:JsonConvert.SerializeObject(p),报循环引用错
[解决办法]
没遇到过,帮顶下
[解决办法]
你的loop是不是圈子比较大啊,不是parent reference child 并且child reference parent.
这里是document中的解释:
Error:
By default Json.NET will error if a reference loop is encountered (otherwise the serializer will get into an infinite loop).
Ignore:
Json.NET will ignore objects in reference loops and not serialize them. The first time an object is encountered it will be serialized as usual but if the object is encountered as a child object of itself the serializer will skip serializing it.
Serialize:
This option forces Json.NET to serialize objects in reference loops. This is useful if objects are nested but not indefinitely.
[解决办法]
加上 [DataContract(IsReference = true)] 就行了。