locked
Cannot deserialize the object RRS feed

  • Question

  • User-1147271863 posted
    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);
    }

    }
    }
    }
    }
    Saturday, December 19, 2020 11:59 PM

All replies

  • User303363814 posted

    What is the source json?  What is the destination object?

    Sunday, December 20, 2020 11:27 PM
  • User1686398519 posted

    Hi pdrake, 

    cannot deserialize the current json object

    1. 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])
    2. If the above solutions cannot solve the problem, can you provide json data?

    Best Regards,

    YihuiSun

    Monday, December 21, 2020 7:29 AM