JSON.NET Error Self referencing loop detected for type

后端 未结 25 2864
我在风中等你
我在风中等你 2020-11-22 02:16

I tried to serialize POCO class that was automatically generated from Entity Data Model .edmx and when I used

JsonConvert.SerializeObject 

25条回答
  •  旧巷少年郎
    2020-11-22 02:22

    The fix is to ignore loop references and not to serialize them. This behaviour is specified in JsonSerializerSettings.

    Single JsonConvert with an overload:

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

    Global Setting with code in Application_Start() in Global.asax.cs:

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

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

提交回复
热议问题