I have a List that looks like:
List<Product> products = new List<Product>();
Product p1 = new Product(1, "Apple", new Description("Red Apple"))
Product p2 = new Product(2, "Banana", new Description("Yellow Banana"))
products.Add(p1);
products.Add(p2);
A product looks like this:
//Product model
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Description descriptions { get; set; }
//Description model
public string description { get; set }
Now I want to serialize this List<Product>
to JSON with JSON.NET. I've tried:
var json = JsonConvert.SerializeObject(products);
But I get the following error:
Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'Module' with type 'System.Reflection.RuntimeModule'.
I also have the following line in my Startup.cs
file that should avoid loops:
xy.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
Any ideas what I'm doing wrong? Can I provide more/better informations? Thanks in advance:)
You should use JsonConvert
's default Setting rather than SerializerSettings
:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.