I am having trouble deserializing a json api. This is my api endpoint: https://www.googleapis.com/books/v1/volumes?q=harry+potter
The problem I have is: The JSON value could not be converted to System.Collections.Generic.IEnumerable at LineNumber: 0 | BytePositionInLine:1
failing at: Books = await JsonSerializer.DeserializeAsync<IEnumerable<Book>>(responseStream);
I think the reason is it is starting the parsing from the root, where it is receiving an object. Is there a way to skip the "kind" and "totalItems" node and start directly from the "items" node?
public async Task<IActionResult> Index()
{
var message = new HttpRequestMessage();
message.Method = HttpMethod.Get;
message.RequestUri = new Uri(URL);
message.Headers.Add("Accept", "application/json");
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(message);
if (response.IsSuccessStatusCode)
{
using var responseStream = await response.Content.ReadAsStreamAsync();
Books = await JsonSerializer.DeserializeAsync<IEnumerable<Book>>(responseStream);
}
else
{
GetBooksError = true;
Books = Array.Empty<Book>();
}
return View(Books);
}
Model Class:
public class Book
{
[Display(Name = "ID")]
public string id { get; set; }
[Display(Name = "Title")]
public string title { get; set; }
[Display(Name = "Authors")]
public string[] authors { get; set; }
[Display(Name = "Publisher")]
public string publisher { get; set; }
[Display(Name = "Published Date")]
public string publishedDate { get; set; }
[Display(Name = "Description")]
public string description { get; set; }
[Display(Name = "ISBN 10")]
public string ISBN_10 { get; set; }
[Display(Name = "Image")]
public string smallThumbnail { get; set; }
}