I want to iterate through my JSON response in viewmodel MVC so I am trying to incorporate IEnumerable for this:
ArtistInfoResponse IMusicRepository.GetArtistResponse(string artistName)
{
artistName = (char)34 + artistName + (char)34;
RestClient client = new RestClient($"https://api.deezer.com/search?q=artist:{artistName}");
RestRequest request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
// Deserialize the string content into JToken object
var content = JsonConvert.DeserializeObject<JToken>(response.Content);
// Deserialize the JToken object into our ArtistInfoResponse Class
return content.ToObject<ArtistInfoResponse>();
}
return null;
}
And I get this response:
From this Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'MyMusicApp.Models.ArtistInfoResponse' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data'.'
Here is my class:
public class ArtistInfoResponse : IEnumerable
{
public SongInfo[] Data { get; set; }
public int Total { get; set; }
public string Next { get; set; }
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}