-3

I want to take the data from the web api service of the Gutenberg project and make a site in an mvc structure. But I encountered an error while pulling the data. I need help. Web API: https://gutendex.com/

`

public async Task<IActionResult> GetBooksFromApi()
        {
            var books = new List<BookModel>();

            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync("https://gutendex.com/books"))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    books = JsonConvert.DeserializeObject<List<BookModel>>(apiResponse);
                }
            }
            return View(books);
        }

`

public class BookModel
    {
        public int id { get; set; }
        public string Title { get; set; }
        public string[] Subjects { get; set; }
        public string[] Authors { get; set; }
        public string[] Translators { get; set; }
        public string[] Bookshelves { get; set; }
        public string[] Languages { get; set; }
        public bool Copyright { get; set; }
        public string Media_Type { get; set; }
        public string Formats { get; set; }
        public int Download_Count { get; set; }
    }
}

screenshot of the error

I wanted to use the json data received using the Newtonsoft package on my site, but I am getting an error in the conversion.

Goktug
  • 37
  • 6
  • What data is in the response? – M. Rogers Nov 29 '22 at 21:48
  • Duplicate [Cannot deserialize the JSON array (e.g. \[1,2,3\]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly](https://stackoverflow.com/questions/22557559/cannot-deserialize-the-json-array-e-g-1-2-3-into-type-because-type-requ). Your model doesnt match the json – Ňɏssa Pøngjǣrdenlarp Nov 29 '22 at 21:49
  • The api returns an object and you are trying to deserialize into an array. Go _look_ at the content returned by the API...it doesn't match what you are expecting. – David L Nov 29 '22 at 21:49

1 Answers1

0

if you need just a collection of books you can extract it using Parse and after this to deserialize to a list. Fix your BookModel class too

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

List<BookModel> results =JObject.Parse(json)["results"].ToObject<List<BookModel>>();


public class BookModel
{
    public int id { get; set; }
    public string Title { get; set; }
    public string[] Subjects { get; set; }
    public object[] Authors { get; set; }
    public string[] Translators { get; set; }
    public string[] Bookshelves { get; set; }
    public string[] Languages { get; set; }
    public bool Copyright { get; set; }
    public string Media_Type { get; set; }
    public object Formats { get; set; }
    public int Download_Count { get; set; }
}

And I recommend to create custom classes instead of object for Authors and Formats properties

Serge
  • 40,935
  • 4
  • 18
  • 45