首页 新闻 会员 周边

.net core json转对象失败,怎么解决

0
悬赏园豆:5 [已解决问题] 解决于 2020-08-29 15:38

//类
public class User1
{
public int id { get; set; }
public string name { get; set; }
}
//方法
[HttpPost, Route("api/OrderPayJs/Notify0")]
public string Notify0([FromBody]User1 u)
{
return "1";
}
//传入参数
{
id: 1,
name: "小新"
}

如果我传入的 json如下
{
id: "1",
name: "小新"
}
也就是id作为字符串传入,方法 Notify0 则会转化user1 失败 得到的u是null
怎么解决呢?

simadi的主页 simadi | 初学一级 | 园豆:134
提问于:2020-07-18 23:22
< >
分享
最佳答案
0

要么你严格按照类型传参,要么用dudu给你的方法

收获园豆:3
MrNice | 老鸟四级 |园豆:3450 | 2020-07-20 12:07
其他回答(2)
0

没明白你想干啥,既然id是int,你当然不能id: "1",这么传入啊,那就是字符串了啊。。。

收获园豆:1
顾晓北 | 园豆:10844 (专家六级) | 2020-07-19 09:48
0

.net core 内置的 System.Text.Json 不能兼容这种情况,Newtonsoft.Json 是能兼容的。

<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.5" />
services.AddMvc().AddNewtonsoftJson();
收获园豆:1
dudu | 园豆:30994 (高人七级) | 2020-07-19 11:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册