1

I have a problem deserializing JSON responses from the RIOT API in C#. I want to get a list of LeagueEntryDTO. The API return a stream looks like this :

[
{
"leagueId": "c83a16ca-b80e-4456-9f57-bc0f2a6020ae",
"queueType": "RANKED_SOLO_5x5",
"tier": "DIAMOND",
"rank": "II",
"summonerId": "XrhB60yIVT_t6Uwp0XuWRpFYD49_Ypk9ycybSdt6LS9Lv5E",
"summonerName": "MasterPrzecin",
"leaguePoints": 7,
"wins": 68,
"losses": 62,
"veteran": false,
"inactive": false,
"freshBlood": false,
"hotStreak": false},
{
"leagueId": "6b3e4e2e-cc90-4e06-afb2-7c2f4b0be9ab",
"queueType": "RANKED_FLEX_SR",
"tier": "PLATINUM",
"rank": "III",
"summonerId": "XrhB60yIVT_t6Uwp0XuWRpFYD49_Ypk9ycybSdt6LS9Lv5E",
"summonerName": "MasterPrzecin",
"leaguePoints": 58,
"wins": 7,
"losses": 7,
"veteran": false,
"inactive": false,
"freshBlood": false,
"hotStreak": false
}
]

This is my current code to transform the data which is failing:

public class LeagueEntryDTOService : ILeagueEntryDTOService
{

    RestClient client = new RestClient("https://eun1.api.riotgames.com/lol/league/v4/entries/");

    public LeagueEntryDTOResponse GetLeagueEntryDTO()
    {
        var request = new RestRequest("by-summoner/{encryptedSummonerId}")
            .AddParameter("encryptedSummonerId", "XrhB60yIVT_t6Uwp0XuWRpFYD49_Ypk9ycybSdt6LS9Lv5E", ParameterType.UrlSegment)
            .AddParameter("api_key", "key");

        var response = client.Execute<LeagueEntryDTOResponse>(request);

        if (!response.IsSuccessful)
        {
            return null;
        }
        return response.Data;
    }
    public class LeagueEntryDTOResponse
    {
        public ICollection<AllRanks> Ranks { get; set; }
    }
    public class AllRanks
    {
        public string Tier { get; set; }
        public string Rank { get; set; }
        public int LeaguePoints { get; set; }
        public int Wins { get; set; }
        public int Losses { get; set; }
    }
}
}

What is a good way to deserialize this data?

0

1 Answer 1

1

You have a list of AllRanks instead of an object that contains a list of AllRanks. If there was a property name before the list array, then you would go about deserializing the json off of LeagueEntryDTOResponse but in this case, List<AllRanks> will do

Try this,

client.Execute<List<AllRanks>>(request)

Also, your class attributes must match the case of the json properties. You can either match the case when declaring the variables or you can add [JsonProperty()] to define the exact match. Since convention is to use first letter as UpperCase, its best to define the JsonProperties like below.

public class AllRanks
{
    [JsonProperty("tier")]
    public string Tier { get; set; }
    [JsonProperty("rank")]
    public string Rank { get; set; }
    [JsonProperty("leaguePoints")]
    public int LeaguePoints { get; set; }
    [JsonProperty("wins")]
    public int Wins { get; set; }
    [JsonProperty("losses")]
    public int Losses { get; set; }
}

Since you are returning List<AllRanks>, you will need to change the return type of the method as well,

public List<AllRanks> GetLeagueEntryDTO()
Sign up to request clarification or add additional context in comments.

1 Comment

@d0natxd see my last line of the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.