json - 在双方都使用 C# 将对象列表发送到 web api 方法-JSON 作为 NULL 传入。?
问题描述
我无法使用下面的代码让 JSON 通过 API 方法。我尝试将它作为 JSON 字符串、列表和内容字符串发送,但所有 3 都在 API 端给了我 null JSON。
这是列表的 JSON
[
{"Id":19,"ScanMode":"SHRINK_SCAN","itemcode":"01600027746\r\n","itemname":"BISQUICK GLUTEN FREE","FriendlyDisplayName":"UPC/PLU: 01600027746\r\n Prod Desc: BISQUICK GLUTEN FREE","Quantity":1,"QuantityDisplay":"Quantity: 1","StoreNumber":"71","DateTimeOfScan":"2020-10-05T22:51:55.392313","UserId":"2aebb04e","UserName":"brian","departmentid":"8","departmentname":"Grocery","vendorid":"","VendorName":"Not Available","ReasonCode":"","Weight":0.0,"retailprice":2.99,"isweight":false,"TotalKLAmount":0.0},
{"Id":20,"ScanMode":"SHRINK_SCAN","itemcode":"01600027746\r\n","itemname":"BISQUICK GLUTEN FREE","FriendlyDisplayName":"UPC/PLU: 01600027746\r\n Prod Desc: BISQUICK GLUTEN FREE","Quantity":1,"QuantityDisplay":"Quantity: 1","StoreNumber":"71","DateTimeOfScan":"2020-10-05T22:52:33.752041","UserId":"2aebb04e","UserName":"brian","departmentid":"8","departmentname":"Grocery","vendorid":"","VendorName":"Not Available","ReasonCode":"","Weight":0.0,"retailprice":2.99,"isweight":false,"TotalKLAmount":0.0},
{"Id":21,"ScanMode":"SHRINK_SCAN","itemcode":"01600027746\r\n","itemname":"BISQUICK GLUTEN FREE","FriendlyDisplayName":"UPC/PLU: 01600027746\r\n Prod Desc: BISQUICK GLUTEN FREE","Quantity":1,"QuantityDisplay":"Quantity: 1","StoreNumber":"71","DateTimeOfScan":"2020-10-05T22:52:08.589202","UserId":"2aebb04e","UserName":"brian","departmentid":"8","departmentname":"Grocery","vendorid":"","VendorName":"Not Available","ReasonCode":"","Weight":0.0,"retailprice":2.99,"isweight":false,"TotalKLAmount":0.0}
]
以下是尝试发送 JSON 的 Android 应用程序的代码 - 将列表发送到 API:
public async Task<string> SubmitKnownLossItemsAsync(List<Item> items, int companyId)
{
string token = Constants.PrivateToken;
string returnVal = string.Empty;
try
{
//i tried both these approaches sending as jsonstring and sending as StringContent
string jsonItems = JsonConvert.SerializeObject(items);
StringContent content = new StringContent(jsonItems, Encoding.UTF8, "application/json");
string url = string.Format(Constants.POSTKNOWNLOSS_ResultURL, companyId, token);
Uri uri = new Uri(url);
HttpResponseMessage response = null;
//none of these 3 trys worked...
//response = await client.PostAsJsonAsync(uri, content);
//response = await client.PostAsJsonAsync(uri, jsonItems);
response = await client.PostAsJsonAsync<List<Item>>(uri, items);
if (response.IsSuccessStatusCode)
{
returnVal = "Success";
}
}
catch (Exception ex)
{
//add logging to log the exception
returnVal = string.Format("ERROR {0}", ex.Message);
returnVal = "Failure";
}
return returnVal;
}
API 代码:
[AcceptVerbs("POST")]
public IHttpActionResult PostShrinkItems(int companyId, string APIToken, [FromBody] string jsonItems)
{
SQLMethods sql = new SQLMethods();
string returnValue = string.Empty;
bool response = sql.ValidateAPIToken(APIToken);
if (!response)
{
throw new Exception("Unauthorized Request - invalid APIToken provided.");
}
else
{
returnValue = sql.InsertShrinkData(jsonItems, companyId);
}
return Ok(returnValue);
}
解决方案
我发现了这个问题并让它现在工作......
webapi方法的签名错误:
这就是签名需要让它工作的方式——它现在工作得很好。
[AcceptVerbs("POST")] public IHttpActionResult PostShrinkItems(int companyId, string APIToken, [FromBody]List jsonItems)
--在 [FromBody] [FromBody]List jsonItems 之后缺少列表
谢谢布赖恩
推荐阅读
- html - 将 div 对齐到 body 的顶部
- python - Python scipy simplex方法不返回基索引
- json - 我想创建一个网络应用程序,它会询问您正在玩哪些游戏,然后添加它们,然后它会建议您通过这些游戏进行一些游戏
- flutter - 在警报对话框中使用提供程序不起作用
- api - 驱动 API 更新:
- javascript - React useEffect Api call with fetch 给出 SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 JSON data
- javascript - 带有承诺的 JavaScript 中的事件循环
- javascript - 从 JSON 在反应应用程序中加载自定义主题数据
- python - 我的 getTerm 没有返回用户值,我的方法不完整 python
- javascript - React 表单 onChange 是后面的一个字符