日期:2014-05-17  浏览次数:20392 次

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),报循环引用错

------解决方案--------------------
没遇到过,帮顶下
------解决方案--------------------
引用:
p里面是有loop,但已经加了ReferenceLoopHandling.Ignore不就应该中止了?


你的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.