JSON.Net Self referencing loop detected

前端 未结 11 854
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 04:18

I have a mssql database for my website within 4 tables.

When I use this:

public static string GetAllEventsForJSON()
{
    using (CyberDBDataContext d         


        
11条回答
  •  臣服心动
    2020-11-28 05:06

    You must set Preserving Object References:

    var jsonSerializerSettings = new JsonSerializerSettings
    {
        PreserveReferencesHandling = PreserveReferencesHandling.Objects
    };
    

    Then call your query var q = (from a in db.Events where a.Active select a).ToList(); like

    string jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(q, jsonSerializerSettings);

    See: https://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm

提交回复
热议问题