JSON 值无法转换为 System.Int32 [英] The JSON value could not be converted to System.Int32

查看:56
本文介绍了JSON 值无法转换为 System.Int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将对象的数据发送到我的 Web API.API 接受一个类的参数,其属性为 int 和 string 类型.

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.

这是我的课:

public class deneme
{
   public int ID { get; set; }
   public int sayi { get; set; }
   public int reqem { get; set; }
   public string yazi { get; set; }
}

这是我的 JSON 对象:

This is my JSON object:

{
   "id":0,
   "sayi":"9",
   "reqem":8,
   "yazi":"sss"
}

我希望 api 读取属性sayi";作为整数.但因为它不能,它给出了错误:

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?

推荐答案

对于 Asp.Net Core 3.0,它使用 System.Text.Json 进行序列化和反序列化.

For Asp.Net Core 3.0, it uses System.Text.Json for serialization and deserialization.

对于使用旧行为,您可以通过引用 Json.NET 支持.

For using old behavior, you could use Json.NET in an ASP.NET Core 3.0 project by referencing Json.NET support.

简答:

  1. 安装预览版Microsoft.AspNetCore.Mvc.NewtonsoftJson.
  2. 更改为services.AddControllers().AddNewtonsoftJson();

这篇关于JSON 值无法转换为 System.Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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