首页 > 解决方案 > 从 JSON HTTPPpost 响应反序列化数据表时为空对象

问题描述

我已经成功地将包含 JSON 序列化字典的 POST 请求从 VB.NET 桌面应用程序发送到 azure 函数,如下所示:

            Dim _kvp_dictionary As New Dictionary(Of String, String)
        _kvp_dictionary.Add("account", _Acount)
        _kvp_dictionary.Add("machineid", _machineid)

        Dim uri As String = _URL + "InventoryLogin"
        Dim postResponse As HttpResponseMessage = Await _HttpClient.PostAsJsonAsync(requestUri:=uri,
                                                                                     value:=_kvp_dictionary,
                                                                                     options:=_JsonSerializerOptions)
        postResponse.EnsureSuccessStatusCode()

-- 我的 Azure 函数接收请求并生成包含单个数据表的返回包,如下所示

            DataTable _table = new DataTable("responseValues");
        DataColumn ColumnToken = new DataColumn("token", typeof(string));_table.Columns.Add(ColumnToken);
        DataColumn ColumnMessage = new DataColumn("message", typeof(string));  _table.Columns.Add(ColumnMessage);
        DataRow newRow = _table.NewRow();
        newRow["token"] = _token;
        newRow["message"] = responseMessage.ToString();
        _table.Rows.Add(newRow);

        //Create a new response object to return to the HTTPPost request
        HttpResponseMessage response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.OK);

        //Populate .Content with json formatted data
        response.Content = JsonContent.Create(_table);

        return new OkObjectResult(response);

-- 我的问题是下面的代码片段(处理来自 POST 请求的响应)没有从上面生成的 JSON 包生成数据表。我已经捕获了 response.content 的字符串翻译,并表明它包含一个序列化的数据表——尽管它看起来非常像一个键值对列表。

 'Capture the response as a string to demonstrate that it contains a datatable.
        Dim strRslt = Await postResponse.Content.ReadAsStringAsync
        '   {"version":"1.1","content":{"objectType":"System.Data.DataTable, System.Data.Common, Version=4.2.2.0, Culture=neutral,
        '   PublicKeyToken=b03f5f7f11d50a3a",
        '   "value":[{"token":"a4862dbe338886fb6734","message":"\r\nCONNECTION STATE = \r\nOpen\r\nSUCCESS. Token Created."}],
        '   "headers":[{"key":"Content-Type","value":["application/json; charset=utf-8"]}]},"statusCode":200,
        '   "reasonPhrase":"OK","headers":[],"trailingHeaders":[],"requestMessage":null,"isSuccessStatusCode":true}

        'I expected this line to write the datatable contained as JSON in the postResponse.content
        'into the Rslt variable below.
        Dim Rslt As DataTable = Await postResponse.Content.ReadFromJsonAsync(GetType(System.Data.DataTable))

-- Rslt 仍然是一个空对象,当我希望它包含由我的 azure 函数生成的反序列化数据表时。

我想知道是否有人可以帮助我解决这个问题。提前致谢。

标签: jsonhttp-postdeserialization

解决方案


推荐阅读