简体   繁体   English

JSON 值无法转换为 System.Nullable[System.Int32]

[英]The JSON value could not be converted to System.Nullable[System.Int32]

I updated an ASP.NET Core 2.2 API to ASP.NET Core 3.0 and I am using System.Json:我将 ASP.NET Core 2.2 API 更新为 ASP.NET Core 3.0,我正在使用 System.Json:

services
  .AddMvc()
  .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
  .AddJsonOptions(x => {}) 

I then tried to post JSON data using Angular 8, which was working before:然后我尝试使用 Angular 8 发布 JSON 数据,这在之前是有效的:

{
  "name": "John"
  "userId": "1"
}

The model in the ASP.NET Core 3.0 API is: ASP.NET Core 3.0 API中的model为:

public class UserModel {
  public String Name { get; set; }
  public Int32? UserId { get; set; } 
}

And the API Controller action is as follows:而API Controller动作如下:

[HttpPost("users")]
public async Task<IActionResult> Create([FromBody]PostModel) { 
}

When I submit the model I get the following error:当我提交 model 时,出现以下错误:

The JSON value could not be converted to System.Nullable[System.Int32]. 

Do I need to do something else when using System.Json instead of Newtonsoft?使用 System.Json 而不是 Newtonsoft 时,我是否需要做其他事情?

Microsoft has removed Json.NET dependency from ASP.NET Core 3.0 and using System.Text.Json namespace now for serialization, deserialization and more. Microsoft 已从 ASP.NET Core 3.0 中删除 Json.NET 依赖项,并使用 System.Text.Json 命名空间现在进行序列化、反序列化等。

You can still configure your application to use Newtonsoft.Json.您仍然可以将应用程序配置为使用 Newtonsoft.Json。 For this -为了这 -

  1. Install Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package

  2. In ConfigureServices() add a call to AddNewtonsoftJson()-在 ConfigureServices() 中添加对 AddNewtonsoftJson() 的调用-

    services.AddControllers().AddNewtonsoftJson();

Read more on https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/阅读更多关于https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to

Here Via the json you pass a string value for UserId but your model refer a int32?在这里,通过 json 您为 UserId 传递一个字符串值,但您的 model 引用一个 int32? value for UserId.用户 ID 的值。 Then how your value convert from string to int32?那么你的值是如何从字符串转换为 int32 的呢?

I faced this issue: and all those solutions did not work for me!我遇到了这个问题:所有这些解决方案都不适合我! so I did the following:-所以我做了以下事情:-

  • First of all the data returned to me as the following:-首先返回给我的数据如下:-
    在此处输入图像描述

    I need to make year as an int and also want to make value as double!我需要将 year 设为 int 并且还希望将 value 设为 double! 在此处输入图像描述
    You should make custom JsonConverter, it worked for me after a lot of search, and here is sample of:-您应该制作自定义 JsonConverter,经过大量搜索后它对我有用,这里是示例:-

StringToDoubleConverter StringToDouble转换器

public sealed class StringToDoubleConverter : JsonConverter<double>
{
    public override double Read(
        ref Utf8JsonReader reader,
        Type typeToConvert, 
        JsonSerializerOptions options)
    {
        double.TryParse(reader.GetString(),out double value);
        return value;
    }

    public override void Write(
        Utf8JsonWriter writer,
        double value, 
        JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }
}

Then you can save it to db!然后就可以保存到db了! play around as you like随心所欲地玩耍

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型&#39;System.Int16&#39;的对象不能转换为类型&#39;System.Nullable`1 [System.Int32] - Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32] JSON 值无法转换为 System.Int32 - The JSON value could not be converted to System.Int32 从“System.Int32”到“System.Nullable”1[[System.Int32, mscorlib]] 的无效转换 - Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib]] LINQ to Entities无法识别方法&#39;&lt;&gt; f__AnonymousType9`2 [System.Nullable`1 [System.Int32],System.Nullable` - LINQ to Entities does not recognize the method '<>f__AnonymousType9`2[System.Nullable`1[System.Int32],System.Nullable` 通过 AJAX 将 JSON 数据发布到 Web API 失败,并显示“无法将 JSON 值转换为 System.Int32” - Posting JSON data via AJAX to Web API fails with "The JSON value could not be converted to System.Int32" 代码优先迁移种子错误:未为类型&#39;System.Nullable&#39;1 [System.Int32]&#39;和&#39;System.Int32&#39;定义二进制运算符Equal - Code First Migration Seed Error: The binary operator Equal is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Int32' 类型&#39;System.Nullable`1 [System.Int32]&#39;的表达式不能用于类型&#39;System.Int32&#39;\\ r \\ n的构造函数参数名称:arguments [0] - Expression of type 'System.Nullable`1[System.Int32]' cannot be used for constructor parameter of type 'System.Int32'\r\nParameter name: arguments[0] C# asp.net 核心 webapi 抛出“JSON 值无法转换为 System.Nullable`1[System.DateTime] - C# asp.net core webapi throwing "The JSON value could not be converted to System.Nullable`1[System.DateTime] LINQ:System.Int32是一个不可为空的值类型 - LINQ: System.Int32 is a non-nullable value type System.Nullable <T> null * int值的值是多少? - System.Nullable<T> What is the value of null * int value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM