如何从.NET Core 3.1中的JSON对象反序列化浮点数据类型

如何从.NET Core 3.1中的JSON对象反序列化浮点数据类型,json,serialization,asp.net-core-3.1,Json,Serialization,Asp.net Core 3.1,我试图将下面的JSON对象传递给接受Order类型对象的.NET Core 3.1 API端点 { "OrderType" : "Sale", "Total" : "100" } public class Order (){ public string OrderType {get; set;} public float Total {get;set;} } 这给了我以下的错误。我怎样才能解决这个问题

我试图将下面的JSON对象传递给接受Order类型对象的.NET Core 3.1 API端点

{ 
"OrderType" : "Sale",
    "Total" : "100"
}

public class Order (){
public string OrderType {get; set;}
public float Total {get;set;}
}
这给了我以下的错误。我怎样才能解决这个问题


错误:“JSON值无法转换为System.Single.Path:$.Total | LineNumber:2 | BytePositionLine:21。”

ASP.Net core 3.1不使用Newtonsoft.JSON进行序列化,它使用自己的序列化程序System.Text.JSON

如果要在ASP.NET core 3.1中使用Newtonsoft.JSON

  • 从NuGet软件包下载Microsoft.AspNetCore.Mvc.NewtonsoftJson
  • 在Startup.cs中替换
    services.AddControllers()services.AddControllers().AddNewtonsoftJson()编写代码>
有关System.Text.Json的更多详细信息,请参阅下面的博客


你可以在你的项目中使用。这解决了你的问题吗?@Yinqiu是的,它解决了。非常感谢。