我有以下JSON从Postman发送到我的Web API。
[
{
"salesId": "SO002494"
},
{
"salesId": "SO002496"
}
]
销售模型
public class Sales
{
public string[] salesId { get; set; }
}
[HttpGet("postRouteOrder")]
public async Task<ActionResult<string>>PostRouteOrder([FromBody] Object json)
{
Sales sales = new Sales();
sales.salesId = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json.ToString());
//string[] data = { "SO002604", "SO002606" };
}
我希望sales.salesId
像数据一样。
建议将请求主体参数类型更改为List<SaleInput>
。
using System.Linq;
[HttpPost]
public async Task<ActionResult<string>> PostRouteOrder ([FromBody] List<SaleInput> saleInputs)
{
string[] data = saleInputs.Select(x => x.SalesId).ToArray();
}
public class SaleInput
{
public string SalesId { get; set; }
}
除此之外,您应该反序列化为对象的List
,而不是反序列化为dynamic
,并将SalesId
作为数组。
List<SaleInput> saleInput = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SaleInput>>(/* JSON */);
sales.salesId = saleInputs.Select(x => x.SalesId)
.ToArray();