【发布时间】:2019-12-28 19:31:26
【问题描述】:
我想将对象数据发送到我的 Web API。 API接受一个class的参数,属性是int和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 对象:
{
"id":0,
"sayi":"9",
"reqem":8,
"yazi":"sss"
}
我希望 api 将属性“sayi”读取为整数。 但是因为它不能,所以它给出了错误:
The JSON value could not be converted to System.Int32. Path: $.sayi
我该如何解决这个问题?
【问题讨论】:
-
您不能将字符串值分配给 int 变量,请将字符串强制转换为 int
-
它必须是
"sayi": 9(不带引号),或者你必须将它绑定到一个字符串而不是一个int,然后自己转换它。没有其他选择。 -
在我从 asp.net core 2.2 更新到 3.0 之前,当我输入“9”时它就起作用了。更新后就不行了。
-
当数值高于或低于整数范围时,这也可能是错误消息,您可能需要将其定义为 long、float、double 等。仅供其他人使用谷歌搜索
-
.NET 5 开始支持,请查看答案stackoverflow.com/a/67153702/3799228
标签: c# json asp.net-core-webapi