4

Para quando o retorno é apenas 1 registro utilizo o seguinte:

{
  "id": "27",
  "name": "Daft Punk",
  "link": "https://www.deezer.com/artist/27",
  "share": "http://www.deezer.com/artist/27?utm_source=deezer&utm_content=artist-27&utm_term=767484347_1452533115&utm_medium=web",
  "picture": "https://api.deezer.com/artist/27/image",
  "picture_small": "https://cdns-images.dzcdn.net/images/artist/f2bc007e9133c946ac3c3907ddc5d2ea/56x56-000000-80-0-0.jpg",
  "picture_medium": "https://cdns-images.dzcdn.net/images/artist/f2bc007e9133c946ac3c3907ddc5d2ea/250x250-000000-80-0-0.jpg",
  "picture_big": "https://cdns-images.dzcdn.net/images/artist/f2bc007e9133c946ac3c3907ddc5d2ea/500x500-000000-80-0-0.jpg",
  "nb_album": 27,
  "nb_fan": 2958193,
  "radio": true,
  "tracklist": "https://api.deezer.com/artist/27/top?limit=50",
  "type": "artist"
}

C#

    public async Task<Artist> GetArtist(int artistId)
    {
        string responseContent = await this.ExecuteHttpGet(string.Format("/artist/{0}", artistId));

        Artist artist = JsonConvert.DeserializeObject<Artist>(responseContent);

        artist.CurrentRuntime = this;

        return artist;
    }

Agora tenho o seguinte Json:

{
  "data": [
    {
      "id": "13",
      "name": "Eminem",
      "link": "https://www.deezer.com/artist/13",
      "picture": "https://api.deezer.com/artist/13/image",
      "picture_small": "https://cdns-images.dzcdn.net/images/artist/9118d4ff7f4ef182684a75cd6a200430/56x56-000000-80-0-0.jpg",
      "picture_medium": "https://cdns-images.dzcdn.net/images/artist/9118d4ff7f4ef182684a75cd6a200430/250x250-000000-80-0-0.jpg",
      "picture_big": "https://cdns-images.dzcdn.net/images/artist/9118d4ff7f4ef182684a75cd6a200430/500x500-000000-80-0-0.jpg",
      "nb_album": 33,
      "nb_fan": 7391090,
      "radio": true,
      "tracklist": "https://api.deezer.com/artist/13/top?limit=50",
      "type": "artist"
    },
    {
      "id": "1353250",
      "name": "Eminem ft. - Dr. Dre Tribute Band",
      "link": "https://www.deezer.com/artist/1353250",
      "picture": "https://api.deezer.com/artist/1353250/image",
      "picture_small": "https://cdns-images.dzcdn.net/images/artist//56x56-000000-80-0-0.jpg",
      "picture_medium": "https://cdns-images.dzcdn.net/images/artist//250x250-000000-80-0-0.jpg",
      "picture_big": "https://cdns-images.dzcdn.net/images/artist//500x500-000000-80-0-0.jpg",
      "nb_album": 1,
      "nb_fan": 490,
      "radio": true,
      "tracklist": "https://api.deezer.com/artist/1353250/top?limit=50",
      "type": "artist"
    },
  ],
  "total": 52,
  "next": "https://api.deezer.com/search/artist?q=eminem&index=25"
}

Como faria para recuperar tudo isso?

1 Resposta 1

5

neste caso, você vai precisar de uma classe com a mesma estrutura do seu JSON, por exemplo:

public class Discografia
{
    public Artist[] data { get; set; }
    public int total { get; set; }
    public string next { get; set; }
}

Então basta desserializar a string dentro desta classe:

public async Task<Discografia> GetDiscografia(int discografiaId)
{
    string responseContent = await this.ExecuteHttpGet(string.Format("/discografia/{0}", discografiaId));

    Discografia discografia = JsonConvert.DeserializeObject<Discografia>(responseContent);
    foreach(var artist in discografia.data)
    {
        artist.CurrentRuntime = this;
    }
    return discografia;
}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .