Trying to deserialize the JSON returned from an API. The response has the following format:

{  
 "items":[  
  {  
     "candidateId":40419,
     "firstName":"Adelaida",
     "lastName":"Banks",

  }
   ....
 ]
}

I am using HttpClient as follows to call the API:

  List<Candidate> model1 = null;

  client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "00000");
  HttpResponseMessage response = await client.GetAsync(MyURL);
  response.EnsureSuccessStatusCode();
  var responseBody = await response.Content.ReadAsStringAsync();


   model1 = JsonConvert.DeserializeObject<List<Candidate>>(responseBody);

And the Class Candidate is defined as follows:

  public class Candidate
{
    public string candidateId { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
    public int phone { get; set; }
    public int mobile { get; set; }

}

But i am getting the exception:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[AirCall.Controllers.Candidate]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

Wonder if it is because the list of elements in the response is within the "Items" element? Any Ideas?

2 Answers

1
Darkonekt On Best Solutions

Your model needs to look like this

   public class Model
  {
    public List<Candidate> items { get; set; }
  }
  public class Candidate
  {
    public string candidateId { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
    public int phone { get; set; }
    public int mobile { get; set; }

  }

And you need to deserialize it like this

model1 = JsonConvert.DeserializeObject<Model>(responseBody);

Where model1 is an instance of Model.

Basically your model does not match the json.

"items" is a property in the json response you are showing.

0
Sara Zakizadeh On

I do something like this :

  using (var client = new HttpClient())
            {
                var apiUrl = _config["MicroService:Base"] + string.Format("/Exam/{0}", examId);

                var response = client.SendAsync(new HttpRequestMessage(HttpMethod.Get, apiUrl))
                    .Result;

                if (!response.IsSuccessStatusCode)
                    return Task.FromCanceled<ExamDetails>(new CancellationToken(true));

                var content = response.Content.ReadAsStringAsync().Result;
                return Task.FromResult((DtoExamDetails)JsonConvert.DeserializeObject(content,
                    typeof(ExamDetails)));
            }

and my model is like this:

 public class ExamDetails
{
    public int Id { get; set; }
    public string Title { get; set; }
    public long CreateBy { get; set; }
    public string CreateByName { get; set; }
    public long CreateDate { get; set; }

}

even you can use a list like this:

<List<ExamDetails>> instead of <ExamDetails>