Ошибка десериализации JSON
Всем привет. Помогите, пожалуйста поборать проблему:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array
(e.g. [1,2,3]) into type 'Microsoft.AspNetCore.Mvc.ActionResult`1[System.Collections.Generic.List`1[APRF.HelpService.ApiContract.Models.Responses.GeneralReportResponse]]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
Имеется 2 сервиса.
- Общий рабочий сервис
- Сервис с отчетами
Чтобы не копировать сервис с отчетами в рабочий сервис. В Сервисе с отчетами была добавлена API, и упакована в пакет.
Пакет добавлен в общий сервис. И через API в пакете этот сервис получает данные отчетов от сервиса отчетов.
Так как API подключается через пакет, то пройтись дебагом и посмотреть что не так не получается.
Так выглядит сам запрос
public static async Task<TResponse> MakePostAsync<TResponse>(this HttpClient client, string relativeUrl, string token, object request = null, Func<HttpResponseMessage, Task<TResponse>> onResponse = null)
{
string url = UrlHelper.Combine(client.BaseAddress!.ToString(), relativeUrl);
string content = JsonConvert.SerializeObject(request);
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url);
if (!string.IsNullOrEmpty(token))
{
httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
httpRequestMessage.Content = new StringContent(content, Encoding.UTF8, "application/json");
using HttpResponseMessage response = await client.SendAsync(httpRequestMessage);
if (onResponse != null)
{
TResponse val = await onResponse(response);
if (val != null)
{
return val;
}
}
await CheckBadRequest(url, response);
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
if (typeof(TResponse) == typeof(string))
{
return (TResponse)(object)Encoding.UTF8.GetString(bytes);
}
return JsonConvert.DeserializeObject<TResponse>(Encoding.UTF8.GetString(bytes));
}
Данные приходят в таком виде (напрямую получил их у сервиса отчетов через постман)
[
{
"id": "91244a5e-ea9d-4963-85dd-d39586b070bd",
"date": "2023-04-06T12:55:44.580277Z",
"callTime": "00:00:00",
"phoneNumber": null,
"number": "A26-1T",
"operator": "Nosova Юлия",
"aon": null,
"phone": "+7 (104)",
"mobile": null,
"fax": "+7 (105)",
"zip": "0000",
"region": "",
"rayon": "",
"fullAddress": " ",
"annotation": null,
},
{
"id": "91244a5e-ea9d-4963-85dd-d39586b070bd",
"date": "2023-04-06T13:01:34.383602Z",
"callTime": "00:00:00",
"phoneNumber": null,
"number": "A26-2T",
"operator": "Nosova Юлия",
"aon": null,
"phone": "+7 (104)",
"mobile": null,
"fax": "+7 (105)",
"zip": "0000",
"region": "",
"rayon": "",
"fullAddress": " ",
"annotation": null,
} ]
Не понятно что не так с JSON Модели вроде как и в сервисе отчетов и в рабочем сервисе идентичны
Вроде в тексте ошибки есть рекомендация по исправлению, но все равно ничего не понятно На сколько я понимаю текст ошибки - приходит массив JSON, а принимаемый тип не массив. Хотя по коду у меня стоит List
async Task<ActionResult<List<GeneralReportResponse>>> IReportApiClient.GetGeneralReportAsync(GetGeneralReportApiRequest vm, string token)
{
return await HttpClient.MakePostAsync<ActionResult<List<GeneralReportResponse>>>("api/Report/GetGeneralReport", token, vm);
}