System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cyc
系统文本Json。JsonException:检测到可能的对象循环。这可能是由于循环造成的,或者如果对象深度大于允许的最大深度32。考虑使用引用处理程序。在JsonSerializerOptions上保留以支持循环。
在系统中。文本Json。ThrowHelper。ThrowJsonException_SerializerCycledtected(Int32 maxDepth)
这是由于进行了对象嵌套或ef 查询进行了预加载(关联加载)引起的,就像解释说的“对象循环”,层级太深解析不了。
解决方法:
- 添加 Microsoft.AspNetCore.Mvc.NewtonsoftJson 包
- Startup中添加服务,忽略循环引用
//解决Json序列化的时候没有对中文进行处理 services.AddControllersWithViews().AddJsonOptions(cfg => { cfg.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); }).AddNewtonsoftJson(option => option.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore );
或者
通过另外创建一个类对查询到的数据进行封装后再返还。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)