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

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

问题描述

我想将对象的数据发送到我的Web API.该API接受一个class参数,其属性是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.

对于使用旧行为,您可以通过引用

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();
  1. Install Microsoft.AspNetCore.Mvc.NewtonsoftJson which is preview version.
  2. Change to services.AddControllers().AddNewtonsoftJson();

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

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