[英]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 -
为了这 -
Install Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package
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.