[英]How to pass json array as a string to web api from postman
我在 postman 中的 Json [Body] 结构如下所示
[{
"SerialNumber": "dev1",
"CustomerId": "cust1",
"Seed": "c393d900-2f25-819f-cc83-0721357bcf10",
"BitLockerKey": null,
"bitLockerRecoveryKey": null
},
{
"SerialNumber": "dev2",
"CustomerId": "cust2",
"Seed": "c393d900-2f25-819f-cc83-0721357bcf22",
"BitLockerKey": null,
"bitLockerRecoveryKey": null
}]
我的 web api function 如下所示,
[HttpPost]
public ActionResult<string> SaveData([FromBody] string Data)
{
int result = _seedService.SeedGeneration(Data);
return result > 0 ? Ok(result) : (ActionResult<string>)UnprocessableEntity("Seed generation
Failed");
}
我的问题是,如何在 postman 中使用 json 数组结构将数据作为字符串传递。 我收到错误“无法将 JSON 值转换为 System.string
使用 JSON.stringify(jsonValue) 您可以将 JSON object 通过 ZDE9B9ED78D7E2E1ZDCEEFFEE71 转换为字符串
结果如下:
"[{"SerialNumber":"dev1","CustomerId":"cust1","Seed":"c393d900-2f25-819f-cc83-0721357bcf10","BitLockerKey":null,"bitLockerRecoveryKey":null},{"SerialNumber":"dev2","CustomerId":"cust2","Seed":"c393d900-2f25-819f-cc83-0721357bcf22","BitLockerKey":null,"bitLockerRecoveryKey":null}]"
您还可以将操作参数声明为 object 的数组,该数组具有 Json 的结构和您为您处理的 Asp.Net 机制。
我已经安装了 Microsoft.AspNetCore.Mvc.NewtonsoftJson package 并修改了 function。 删除字符串作为参数并在参数中包含 JsonElement。
[HttpPost]
public ActionResult<string> SaveData([FromBody] JsonElement Data)
{
string json = System.Text.Json.JsonSerializer.Serialize(Data);
int result = _seedService.SeedGeneration(json);
return result > 0 ? Ok(result) : (ActionResult<string>)UnprocessableEntity("Seed
generation Failed");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.