gpt4 book ai didi

c# - JSON 值无法转换为 System.Int32

转载 作者:行者123 更新时间:2023-12-04 16:19:00 24 4
gpt4 key购买 nike

我想将对象数据发送到我的 Web API。 API 接受类的参数,其属性类型为 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
我怎么能解决这个问题?

最佳答案

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

要使用旧行为,您可以通过引用 Json.NET support 在 ASP.NET Core 3.0 项目中使用 Json.NET。 .

简答:

  • 安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson这是预览版。
  • 更改为 services.AddControllers().AddNewtonsoftJson();
  • 关于c# - JSON 值无法转换为 System.Int32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57626878/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com