1

I am working on an api wrapper for the riot games api. I am running into an issue with the following endpoint:

champion-mastery/v4/champion-masteries/by-summoner/{encryptedSummonerId}

I am using newtonsoft json and RestSharp to get the results. Here is my code.

public Masteries getMasteriesBySummoner(string id, string server)
{
    RestClient client = new RestClient($"https://{getServer(server)}.api.riotgames.com/lol/");
    RestRequest request = new RestRequest($"champion-mastery/v4/champion-masteries/by-summoner/{id}");
    request.AddHeader("X-Riot-Token", key);

    var response = client.Get(request);
    Masteries masteries = JsonConvert.DeserializeObject<Masteries>(response.Content);
    return masteries;
}

This pattern has worked for about 80% of the endpoints. However for this particular endpoint a JSON array is returned. This particular json array also has no key such as 'data' or 'entries' like all the other ones do. So I cant seem to figure out how to make the masteries model correctly. Here is what I have so far

public class Masteries
{
    public int championLevel { get; set; }
    public bool chestGranted { get; set; }
    public int championPoints { get; set; }
    public int championPointsSinceLastLevel { get; set; }
    public int championPointsUntilNextLevel { get; set; }
    public string summonerId { get; set; }
    public int tokensEarned { get; set; }
    public int championId { get; set; }
    public object lastPlayTime { get; set; }
}

which works for one entry, but not for the whole array. Adding a list doesnt seem to work as there is no key to base the list on.

Heres a sample of the JSON Pattern...

[
    {
        "championLevel": 7,
        "chestGranted": false,
        "championPoints": 70594,
        "championPointsSinceLastLevel": 48994,
        "championPointsUntilNextLevel": 0,
        "summonerId": "xaZh_7U2VIRtArKG-mgda_Rt9TSRVRmhBp2GFE88bmTSqJU",
        "tokensEarned": 0,
        "championId": 10,
        "lastPlayTime": 1537676432000
    },
    {
        "championLevel": 7,
        "chestGranted": true,
        "championPoints": 51273,
        "championPointsSinceLastLevel": 29673,
        "championPointsUntilNextLevel": 0,
        "summonerId": "xaZh_7U2VIRtArKG-mgda_Rt9TSRVRmhBp2GFE88bmTSqJU",
        "tokensEarned": 0,
        "championId": 69,
        "lastPlayTime": 1548208513000
    }
]

Any help is appreciated, thanks.

Joe G
  • 99
  • 1
  • 14
  • 1
    Can you show us the JSON? – Enigmativity Jan 26 '19 at 22:58
  • @Enigmativity updated the question to include a sample of the json – Joe G Jan 26 '19 at 23:08
  • 6
    It is a Json **array**; in other words, a list/collection of Json objects representing multiple Masteries. Thus, deserialize as `Masteries[]` array, `List` or an equivalent collection... –  Jan 26 '19 at 23:09
  • @elgonzo - Why not post this as an answer? – Enigmativity Jan 27 '19 at 00:09
  • Looks like a duplicate of [Cannot deserialize the JSON array (e.g. `[1,2,3]`) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly](https://stackoverflow.com/q/22557559/3744182) and/or [Deserializing JSON Object Array with Json.net](https://stackoverflow.com/q/18192357/3744182). – dbc Jan 27 '19 at 21:19

0 Answers0