0

I am trying to use RIOT API to read Summoners info, but I got stuck right after the deserialization. When I am trying to save the JSON properties into an object, it just doesn't save any of the properties.

model:

public class LOLSummoner
{
    [JsonProperty("id")]
    public string id { get; set; }
    [JsonProperty("accountId")]
    public string accountId { get; set; }
    [JsonProperty("puuid")]
    public string puuid { get; set; }
    [JsonProperty("name")]
    public string name { get; set; }
    [JsonProperty("profileIconId")]
    public string profileIconId { get; set; }
    [JsonProperty("revisionDate")]
    public string revisionDate { get; set; }
    [JsonProperty("summonerLevel")]
    public string summonerLevel { get; set; }
}

readapi method:

public async void OnPostCallAPI()
{
    string Baseurl = "https://eun1.api.riotgames.com/lol/summoner/v4/summoners/by-name/PolskaHrozba123";

    try
    {
        using (var client = new HttpClient())
        {
            HttpRequestMessage request = new HttpRequestMessage();
            request.RequestUri = new Uri(Baseurl);
            request.Method = HttpMethod.Get;
            request.Headers.Add("X-Riot-Token", "RGAPI-020de469-e1b9-4bdf-aea1-175739868a0b");
            HttpResponseMessage response = await client.SendAsync(request);         
            var responseString = await response.Content.ReadAsStringAsync();
            var statusCode = response.StatusCode;
            
            if (response.IsSuccessStatusCode)
            {
                var responses = JsonConvert.DeserializeObject<LOLSummoner>(responseString);
                
                LOLSummoner TestovaciUzivatel = new()
                {
                    id = responses.id,
                    accountId = responses.accountId,
                    puuid = responses.puuid,
                    name = responses.name,
                    profileIconId = responses.profileIconId,
                    revisionDate = responses.revisionDate,
                    summonerLevel = responses.summonerLevel
                };
                
                summonername = TestovaciUzivatel.name.ToString();             
            }
            else
            {              
            }
        }
    }
    catch (Exception ex)
    {

        throw;
    }
}

If you see any mistakes let me know! Gotta find a solution.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Drapys
  • 52
  • 8
  • The json part will be needed. Save it as string . then edit it to trim everything that does not cause issue. When you narrowed down to a few properties/ level you will an answerable question, and perhaps an answer of your own . – Drag and Drop Jun 20 '23 at 07:43
  • What json part will be needed? – Drapys Jun 20 '23 at 07:53
  • Well after testing you hav no issue with the Json nor the deserialisation. But the question seems to be all about Json and deserialisation.. – Drag and Drop Jun 20 '23 at 08:07
  • Well from my perspective theres a problem with json and deseralisation, idk, im trying to get answers, what are you trying to tell me? – Drapys Jun 20 '23 at 08:09
  • 1
    I'm saying i slapped the code in an IDE online https://dotnetfiddle.net/e4r2dz. Removed the async , and dump the result to console and I have a populated object. You can try to add `Debug.WriteLine(responses .id);` you should have a value – Drag and Drop Jun 20 '23 at 08:45

1 Answers1

1

The problem was in async, after removing async from the method, it somehow actually saved the info in the object. No clue why it didnt with async.

Thank you @Drag and Drop for helping! :)

Drapys
  • 52
  • 8