Cannot deserialize the current JSON array.

Rajesh Somvanshi 1 Reputation point
2022-01-21T11:41:00.967+00:00

Dear Friends,

Good Evening

I am getting an error in the below source code.
Could you please help me with this.

Error details

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'LSI.Web.Models.ResponseDto' 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.'

Error Line
var apiRespnseDto = JsonConvert.DeserializeObject<T>(apiContent); // Error Line

Code details are below

public async Task<T> SendAsync<T>(ApiRequest apiRequest)
{
try
{
var client = httpClient.CreateClient("LSIAPI");
HttpRequestMessage message = new HttpRequestMessage();
message.Headers.Add("Accept", "application/json");
message.RequestUri = new Uri(apiRequest.Url);
client.DefaultRequestHeaders.Clear();
if (apiRequest.Data != null)
{
message.Content = new StringContent(JsonConvert.SerializeObject(apiRequest.Data),
Encoding.UTF8, "application/json");
}

            HttpResponseMessage apiResponse = null;  
            switch (apiRequest.ApiType)  
            {  
                case SD.ApiType.POST:  
                    message.Method = HttpMethod.Post;  
                    break;  
                case SD.ApiType.PUT:  
                    message.Method = HttpMethod.Put;  
                    break;  
                case SD.ApiType.DELETE:  
                    message.Method = HttpMethod.Delete;  
                    break;  
                default:  
                    message.Method = HttpMethod.Get;  
                    break;  
            }  

            apiResponse = await client.SendAsync(message);  
            var apiContent = await apiResponse.Content.ReadAsStringAsync();  
            var apiRespnseDto = JsonConvert.DeserializeObject<T>(apiContent);  
            return apiRespnseDto;  


        }  
        catch (Exception ex)  
        {  
            var dto = new ResponseDto  
            {  
                Displaymessage = "Error",  
                ErrorMessages = new List<string> { Convert.ToString(ex.Message) },  
                IsSuccess = false  
            };  
            var res = JsonConvert.SerializeObject(dto);  
            var apiResponseDto = JsonConvert.DeserializeObject<T>(res);  
            return apiResponseDto.ToString();  

        }  
    }  

167241-one.png

167196-two.png

Thanks In Advance
Rajesh Somvanshi

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-01-21T12:20:31.593+00:00

    You did not share the most import bits of the code. I'm guessing the type T passed to the generic method is a single type while the response is an array.

    2 people found this answer helpful.
    0 comments No comments

  2. Jose Zero 576 Reputation points
    2022-01-29T16:17:42.727+00:00

    @AgaveJoe is right, based on image posted you have a Json Array.
    You have to Deserialize into an Array or List.
    Excetion message is telling you what should be done

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.