I want to send data of object to my Web API. The API accepts a parameter of class, which properties are type of int and string.
This is my class:
public class deneme { public int ID { get; set; } public int sayi { get; set; } public int reqem { get; set; } public string yazi { get; set; } }
This is my JSON object:
{ "id":0, "sayi":"9", "reqem":8, "yazi":"sss" }
I want the api read the property "sayi" as integer. but because it cant, it gives the error:
The JSON value could not be converted to System.Int32. Path: $.sayi
How could I solve this problem?
System.Text.Json.JsonException: ‘The JSON value could not be converted to System.Int32. This issue is more because the new .NET /ASP.NET Core 3.1 framework has removed the dependency on JSON.NET and uses it’s own JSON serializer i.e ‘ System.Text.Json ‘.
System.Text.Json.JsonException: ‘The JSON value could not be converted to System.String. This issue is more because of the new .NET /ASP.NET Core 3.1 framework serializer which has removed the dependency on JSON.NET and uses it’s own JSON serializer i.e ‘ System.Text.Json ‘.
System.Text.Json doesn’t deserialize non-string values like Int, Boolean and other primitives into string properties. System.Text.Json.JsonException: ‘The JSON value could not be converted to System.String
Serialization/Deserialization in .NET Core or ASP.NET Core 3.1 or .NET 5.0 gives below or similar error, System.Text.Json.JsonException: ‘The JSON value could not be converted to System.Int32. InvalidOperationException: Cannot get the value of a token type ‘String’ as a number.
For Asp.Net Core 3.0, it uses System.Text.Json
for serialization and deserialization.
For using old behavior, you could use Json.NET in an ASP.NET Core 3.0 project by referencing Json.NET support.
Short Answer:
Microsoft.AspNetCore.Mvc.NewtonsoftJson
which is preview version.services.AddControllers().AddNewtonsoftJson();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With