gpt4 book ai didi

c# - 将数组 Json 发布到 .net 核心 web api Controller

转载 作者:太空狗 更新时间:2023-10-29 20:35:57 24 4
gpt4 key购买 nike

<分区>

我有这个代码

    [HttpPost("[action]")]
public IActionResult Add([FromBody] Player player)
{
PlayerService.Add(player);
PlayerService.SaveChanges();
return Created("Player created",player.Name);
}

和这个 json

[
{
"name": "Olivier Giroud",
"league": "Premier League",
"currentTeam": "Arsenal"
}
]

一切正常。但是如果我想发送一个json数组

[ 
{ "name": "Olivier Giroud", "league": "Premier League", "currentTeam": "Arsenal" },
{"name": "Lucas Perez","league": "Premier League","currentTeam":"Arsenal"}
]

我得到一个异常“对象引用未设置到对象的实例。”“。我尝试使用 List

public IActionResult Add([FromBody] List< Player >  players)    

或者

public IActionResult Add([FromBody] IEnumerable < Player >  players)

或者

public IActionResult Add([FromBody] Players[] players)

但没有成功。我该怎么办?

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