[英]How can i post multiply objects in array and get the data in the action method
I am using ASP.NET Core 2.1 with Web API and posting works fine when I send one object. 我正在将ASP.NET Core 2.1与Web API一起使用,并且当我发送一个对象时,投递工作正常。 How can I send this type here. 我如何在这里发送此类型。
Until now I have tried this approach, https://ibb.co/dKztDGv , but without success. 到目前为止,我还尝试过https://ibb.co/dKztDGv这种方法,但是没有成功。 I don't get an error but I get the values from the last object only. 我没有收到错误,但仅从最后一个对象获取值。 Please help me get all the values in the array. 请帮助我获取数组中的所有值。
//I post this type of objects in array
[
{something...},
{something...}
]
//what I've tried
public IActionResult PostNewLanguages([FromBody] JObject newLanguages, string id)
{
var oneUser = GetSpecificUser(id);
JObject class1DataJson = newLanguages;
return Ok();
}
When I run this and send an array of objects I get this error 当我运行它并发送对象数组时,出现此错误
InvalidCastException: Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'Newtonsoft.Json.Linq.JObject'. InvalidCastException:无法将类型为“ Newtonsoft.Json.Linq.JArray”的对象转换为类型为“ Newtonsoft.Json.Linq.JObject”的对象。
So I suggest trying to send something like this instead 所以我建议尝试发送类似这样的内容
{ "myData": [
{something...},
{something...}
]}
And then to access the data you will need to do 然后访问数据,您将需要执行
var myValues = newLanguages["myData"];
foreach(var value in myValues)
{
}
'myData' being whatever key you use for your array. “ myData”是您用于数组的任何键。 Looks like 'request' in your case going by your comments. 根据您的评论,您的情况看起来像是“请求”。
If you post an array of object, you need to receive it as a List like 如果您发布对象数组,则需要像列表一样接收它
public IActionResult PostNewLanguages([FromBody] List<JObject> newLanguages, string id)
{
foreach(var obj in newLanguages)
{
}
return Ok();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.