Deserialize Json List

Asked

Viewed 110 times

4

For when the return is only 1 record I use the following:

{
  "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;
    }

Now I have the following 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"
}

How would you get all this back?

1 answer

5


in this case, you will need a class with the same structure as your JSON, for example:

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

Then just deserialize the string within this class:

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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.