asp.net-core - JsonResult(object) 导致“不支持集合类型'Newtonsoft.Json.Linq.JToken'。”
问题描述
我将现有的 ASP.NET Core 2.2 项目升级到 3.0。我有一个返回 JSON 的方法,该方法在 2.2 中有效,但在 3.0 中,它会导致“不支持集合类型‘Newtonsoft.Json.Linq.JToken’。” 在返回。
[HttpGet()]
public async Task<JsonResult> Get()
{
var res = some class object in a third-party library;
return new JsonResult(res);
}
我搜索了谷歌,发现微软已经将 Newtonsoft Json 替换为System.Text.Json
,但我没有明确使用 Newtonsoft Json。在项目的“框架”中,我可以看到 Newtonsoft Json,我删除了它和using Newtonsoft.Json.Linq
语句,但结果是一样的。如何不使用 Newtonsoft Json?
错误信息是:
System.NotSupportedException: The collection type 'Newtonsoft.Json.Linq.JToken' is not supported.
at System.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonConverter converter, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.AddPolicyProperty(Type propertyType, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)
at System.Text.Json.JsonPropertyInfo.get_ElementClassInfo()
at System.Text.Json.JsonSerializer.HandleObject(JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteObject(JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext context, JsonResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultAsync>g__Logged|21_0(ResourceInvoker invoker, IActionResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
PS:我已经尝试过其他类型,并得到了一些类似的集合类型的异常消息。有了这个,我搜索了 Google 并在 .NET Core 的 GitHub 上找到了这个未解决的问题。目前似乎System.Text.JSon
不完全支持集合类型,解决方法是使用旧的 Newtonsoft Json。
解决方案
添加对 Microsoft.AspNetCore.Mvc.NewtonsoftJson 的包引用
更新 Startup.ConfigureServices 以调用 AddNewtonsoftJson:
services.AddMvc().AddNewtonsoftJson();
推荐阅读
- c# - 在 ASP.NET Core 中捕获空闲超时用户详细信息
- r - 执行匹配的风险集抽样(发病密度抽样)而不对两个变量进行替换匹配
- r - 重新格式化ggplot barplot的数据框
- terraform - 嵌套块如何在自定义 terraform 提供程序中具有标签?
- javascript - SVG 动画在 React 中引发“无法读取属性 'style' of null”错误
- realm - 领域查询有时不返回数据
- c - yocto设置的镜像包服务器无法搜索到download/git2/下的包
- pytorch - pytorch cuda 不可用:CUDA 初始化:CUDA 未知错误
- java - 创建模拟对象时Mockito无法访问对象异常
- gradle - gradle DSL to api:从 DSL 到类方法调用的转换是如何完成的?