.NET Core 中 AWS Api 网关对 JSON 的响应
AWS Api gateway response to JSON in .NET Core
我正在开展一个项目,我需要在我的 .NET 核心 API 中使用 AWS API 网关端点。我收到来自 AWS api 的回复,但它是 application/json 格式。
https://hbgamarapi.azurewebsites.net/GetPressValues
AWS SDK 中没有可用的方法将以下响应转换为 JSON。
这是我的控制器代码
var _val = string.Empty;
using (var _client = CreateApiClient.CreateClient(DataUrl.AkranesWheightUser))
{
using (var _reponse = await _client.GetAsync(DataUrl.WheightAkranes))
{
_val = await _reponse.Content.ReadAsStringAsync();
var jsonDoc = Document.FromJson(_val);
var res = jsonDoc.ToJson();
}
}
return _val;
这是来自 AWS api 网关的响应
{"Items": [{"Thyngd": {"N": "16"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1838"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "20/08/19 10:44:40"}}, {"Deild_Nr": {"N": "307"}, "Lota": {"N": "21"}, "Dagur": {"S": "22/06/20 06:21:53"}, "Thyngd": {"N": "33"}, "Tegund": {"S": "Almennt"}, "ID": {"N": "2289"}, "Deild": {"S": "NORDANFISK"}, "FL_Nr": {"N": "500"}}, {"Thyngd": {"N": "200"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1007"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "10/07/18 08:56:01"}}, {"Thyngd": {"N": "63"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1581"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "09/04/19 07:43:16"}}, {"Thyngd": {"N": "237"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1175"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "03/10/18 10:33:58"}}, {"Thyngd": {"N": "341"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1259"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "13/11/18 08:44:09"}}, {"Thyngd": {"N": "8"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1933"}, "Deild": {"S": "Br\u0153\u00f0sla"}, "Dagur": {"S": "25/09/19 9:34:39"}}, {"Thyngd": {"N": "221"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1091"}, "Deild": {"S": "BIAMAR"}, "Dagur": {"S": "20/08/18 08:03:27"}}, {"Thyngd": {"N": "235"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1303"}, "Deild": {"S": "Br\u0153\u00f0sla"}, "Dagur": {"S": "29/11/18 11:36:58"}}, {"Thyngd": {"N": "32"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1892"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "06/09/19 11:19:52"}}, {"Deild_Nr": {"N": "308"}, "Lota": {"N": "21"}, "Dagur": {"S": "20/05/20 08:02:19"}, "Thyngd": {"N": "63"}, "Tegund": {"S": "Almennt"}, "ID": {"N": "2251"},
请提出可能的方法
您可以使用 Newtonsoft
反序列化对象。根据您的请求日期,使用 ExpandoObjectConverter
从 JSON.
转换复杂模型
首先,通过 NuGet
.
安装 Newtonsoft
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
[Route("/GetPressValues")]
public async Task<dynamic> GetJsonAsync()
{
var _val = string.Empty;
dynamic pressValues;
using (var client = _clientFactory.CreateClient())
{
using (var _reponse = await client.GetAsync("https://hbgamarapi.azurewebsites.net/GetPressValues"))
{
_val = await _reponse.Content.ReadAsStringAsync();
var converter = new ExpandoObjectConverter();
//use ExpandoObject whose members can be dynamically added
pressValues = JsonConvert.DeserializeObject<ExpandoObject>(_val, converter);
}
}
return pressValues.Items;//Here you get List of PressValues
}
测试截图:
我正在开展一个项目,我需要在我的 .NET 核心 API 中使用 AWS API 网关端点。我收到来自 AWS api 的回复,但它是 application/json 格式。
https://hbgamarapi.azurewebsites.net/GetPressValues
AWS SDK 中没有可用的方法将以下响应转换为 JSON。
这是我的控制器代码
var _val = string.Empty;
using (var _client = CreateApiClient.CreateClient(DataUrl.AkranesWheightUser))
{
using (var _reponse = await _client.GetAsync(DataUrl.WheightAkranes))
{
_val = await _reponse.Content.ReadAsStringAsync();
var jsonDoc = Document.FromJson(_val);
var res = jsonDoc.ToJson();
}
}
return _val;
这是来自 AWS api 网关的响应
{"Items": [{"Thyngd": {"N": "16"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1838"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "20/08/19 10:44:40"}}, {"Deild_Nr": {"N": "307"}, "Lota": {"N": "21"}, "Dagur": {"S": "22/06/20 06:21:53"}, "Thyngd": {"N": "33"}, "Tegund": {"S": "Almennt"}, "ID": {"N": "2289"}, "Deild": {"S": "NORDANFISK"}, "FL_Nr": {"N": "500"}}, {"Thyngd": {"N": "200"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1007"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "10/07/18 08:56:01"}}, {"Thyngd": {"N": "63"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1581"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "09/04/19 07:43:16"}}, {"Thyngd": {"N": "237"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1175"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "03/10/18 10:33:58"}}, {"Thyngd": {"N": "341"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1259"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "13/11/18 08:44:09"}}, {"Thyngd": {"N": "8"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1933"}, "Deild": {"S": "Br\u0153\u00f0sla"}, "Dagur": {"S": "25/09/19 9:34:39"}}, {"Thyngd": {"N": "221"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1091"}, "Deild": {"S": "BIAMAR"}, "Dagur": {"S": "20/08/18 08:03:27"}}, {"Thyngd": {"N": "235"}, "Tegund": {"S": "-"}, "Lota": {"N": "0"}, "ID": {"N": "1303"}, "Deild": {"S": "Br\u0153\u00f0sla"}, "Dagur": {"S": "29/11/18 11:36:58"}}, {"Thyngd": {"N": "32"}, "Tegund": {"S": "-"}, "Lota": {"N": "18"}, "ID": {"N": "1892"}, "Deild": {"S": "Vignir G. "}, "Dagur": {"S": "06/09/19 11:19:52"}}, {"Deild_Nr": {"N": "308"}, "Lota": {"N": "21"}, "Dagur": {"S": "20/05/20 08:02:19"}, "Thyngd": {"N": "63"}, "Tegund": {"S": "Almennt"}, "ID": {"N": "2251"},
请提出可能的方法
您可以使用 Newtonsoft
反序列化对象。根据您的请求日期,使用 ExpandoObjectConverter
从 JSON.
转换复杂模型
首先,通过 NuGet
.
Newtonsoft
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
[Route("/GetPressValues")]
public async Task<dynamic> GetJsonAsync()
{
var _val = string.Empty;
dynamic pressValues;
using (var client = _clientFactory.CreateClient())
{
using (var _reponse = await client.GetAsync("https://hbgamarapi.azurewebsites.net/GetPressValues"))
{
_val = await _reponse.Content.ReadAsStringAsync();
var converter = new ExpandoObjectConverter();
//use ExpandoObject whose members can be dynamically added
pressValues = JsonConvert.DeserializeObject<ExpandoObject>(_val, converter);
}
}
return pressValues.Items;//Here you get List of PressValues
}
测试截图: