I'm having trouble with resolving the View(Movies). I have debugged this and the problem stems from where I try and add to the list after attempting to deserializing it. The debugger says newtonsoft.json.jsonserlizationexception cannot deserialize the current json object
using System; using System.Collections.Generic; using System.Data; using System.Net; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using pdrake.Models; namespace pdrake.Controllers { public class MovieApiController : Controller { public List<Root> Movies { get; set; } public async Task<ViewResult> GetMovies() { using (var httpClient = new HttpClient()) { using (var response = await httpClient.GetAsync("https://api.themoviedb.org/3/discover/movie?api_key=my_key")) { string apiResponse = await response.Content.ReadAsStringAsync(); Movies = JsonConvert.DeserializeObject<List<Root>>(apiResponse); return View(Movies); } } } } }
It seems that you did not provide all the error messages. Is your error like the following?If it is the following error message, you can
click this link to view the solution.
Cannot deserialize the current json object because xxxx into type
because the type requires a json array (e.g.[1,2,3])
If the above solutions cannot solve the problem, can you provide json data?
Best Regards,
YihuiSun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
1 Post
Cannot deserialize the object
Dec 19, 2020 11:59 PM|pdrake|LINK
Participant
1580 Points
906 Posts
Re: Cannot deserialize the object
Dec 20, 2020 11:27 PM|PaulTheSmith|LINK
What is the source json? What is the destination object?
Contributor
2380 Points
679 Posts
Re: Cannot deserialize the object
Dec 21, 2020 07:29 AM|YihuiSun|LINK
Hi pdrake,
Best Regards,
YihuiSun