JSON.NET Error Self referencing loop detected for type

后端 未结 25 2858
我在风中等你
我在风中等你 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:39

    I had this exception and my working solution is Easy and Simple,

    Ignore the Referenced property by adding the JsonIgnore attribute to it:

    [JsonIgnore]
    public MyClass currentClass { get; set; }
    

    Reset the property when you Deserialize it:

    Source = JsonConvert.DeserializeObject<MyObject>(JsonTxt);
    foreach (var item in Source)
            {
                Source.MyClass = item;
            }
    

    using Newtonsoft.Json;

    0 讨论(0)
  • 2020-11-22 02:40

    C# code:

                var jsonSerializerSettings = new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                    PreserveReferencesHandling = PreserveReferencesHandling.Objects,
                };
    
                var jsonString = JsonConvert.SerializeObject(object2Serialize, jsonSerializerSettings);
    
                var filePath = @"E:\json.json";
    
                File.WriteAllText(filePath, jsonString);
    
    0 讨论(0)
  • 2020-11-22 02:41

    Use this in WebApiConfig.cs class :

    var json = config.Formatters.JsonFormatter;
    json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
    config.Formatters.Remove(config.Formatters.XmlFormatter);
    
    0 讨论(0)
  • 2020-11-22 02:41

    Please also make sure to use await and async in you method. You can get this error if your object are not serialized properly.

    0 讨论(0)
  • 2020-11-22 02:44

    Use JsonSerializerSettings

    • 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.

    Example:

    JsonConvert.SerializeObject(YourPOCOHere, Formatting.Indented, 
    new JsonSerializerSettings 
    { 
            ReferenceLoopHandling = ReferenceLoopHandling.Serialize
    });
    

    Should you have to serialize an object that is nested indefinitely you can use PreserveObjectReferences to avoid a StackOverflowException.

    Example:

    JsonConvert.SerializeObject(YourPOCOHere, Formatting.Indented, 
    new JsonSerializerSettings 
    { 
            PreserveReferencesHandling = PreserveReferencesHandling.Objects
    });
    

    Pick what makes sense for the object you are serializing.

    Reference http://james.newtonking.com/json/help/

    0 讨论(0)
  • 2020-11-22 02:44

    My Problem Solved With Custom Config JsonSerializerSettings:

    services.AddMvc(
      // ...
                   ).AddJsonOptions(opt =>
                     {
                    opt.SerializerSettings.ReferenceLoopHandling =
                        Newtonsoft.Json.ReferenceLoopHandling.Serialize;
                    opt.SerializerSettings.PreserveReferencesHandling =
                        Newtonsoft.Json.PreserveReferencesHandling.Objects;
                     });
    
    0 讨论(0)
提交回复
热议问题