首页 > 解决方案 > 在双方都使用 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);
    }

标签: jsonasp.net-mvcwebapi

解决方案


我发现了这个问题并让它现在工作......

webapi方法的签名错误:

这就是签名需要让它工作的方式——它现在工作得很好。

[AcceptVerbs("POST")] public IHttpActionResult PostShrinkItems(int companyId, string APIToken, [FromBody]List jsonItems)

--在 [FromBody] [FromBody]List jsonItems 之后缺少列表

谢谢布赖恩


推荐阅读