A set of technologies in the .NET Framework for building web applications and XML web services.
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.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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();
}
}
Thanks In Advance
Rajesh Somvanshi
A set of technologies in the .NET Framework for building web applications and XML web services.
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.
@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.