JsonResult(object)导致“不支持集合类型'Newtonsoft.Json.Linq.JToken'." [英] JsonResult(object) causes "The collection type 'Newtonsoft.Json.Linq.JToken' is not supported."

查看:942
本文介绍了JsonResult(object)导致“不支持集合类型'Newtonsoft.Json.Linq.JToken'."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将现有的ASP.NET Core 2.2项目升级到3.0.我有一个返回JSON的方法,该方法在2.2中可用,但是在3.0中,它导致不支持集合类型'Newtonsoft.Json.Linq.JToken'".在返回.

I upgraded an existing ASP.NET Core 2.2 project to 3.0. I have a method that returns JSON which worked in 2.2, but in 3.0, it causes "The collection type 'Newtonsoft.Json.Linq.JToken' is not supported." at the return.

[HttpGet()]
public async Task<JsonResult> Get()
{
    var res = some class object in a third-party library;
    return new JsonResult(res);
}

我搜索了Google,发现Microsoft已将Newtonsoft Json替换为System.Text.Json,但是我没有明确使用Newtonsoft Json.在项目的框架"中,我可以看到Newtonsoft Json,并删除了它和using Newtonsoft.Json.Linq语句,但是结果是相同的.如何不使用Newtonsoft Json?

I have searched Google and found that Microsoft has replaced Newtonsoft Json to System.Text.Json, but I have not explicitly used Newtonsoft Json. In the project's "Frameworks", I could see the Newtonsoft Json, and I removed it and the using Newtonsoft.Json.Linq statements, but the result was the same. How not to use 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.


PS: I have tried it with other types and got some similar exception messages for collection types. With that, I searched Google and found this open issue on .NET Core's GitHub. It seems like System.Text.JSon currently does not support collection types fully, and a workaround is just use the old Newtonsoft Json.

推荐答案

  • 添加对Microsoft.AspNetCore.Mvc.NewtonsoftJson的程序包引用

    • Add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson

    更新Startup.ConfigureServices以调用AddNewtonsoftJson: services.AddMvc().AddNewtonsoftJson();

    Update Startup.ConfigureServices to call AddNewtonsoftJson: services.AddMvc().AddNewtonsoftJson();

    这篇关于JsonResult(object)导致“不支持集合类型'Newtonsoft.Json.Linq.JToken'."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆