0

I'm trying to populate the properties of this class:

public class Summoner   
{
    public int id { get; set; }
    public string name { get; set; }
    public int profileIconId { get; set; }
    public int summonerLevel { get; set; }
    public long revisionDate { get; set; }
}

With this JSON:

{"SummonerName":{"id":445312515,"name":"SummonerName","profileIconId":28,"summonerLevel":30,"revisionDate":140642312000}}

Using JSON.net with the following:

public static Summoner getRecentGames(string summonerId)
    {
        Summoner summoner = new Summoner();
        try
        {
            using (var webClient = new System.Net.WebClient())
            {
                var json = webClient.DownloadString("https://eu.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/"+summonerId+"?api_key="+api_key);
                webClient.Dispose();
                summoner = JsonConvert.DeserializeObject<Summoner>(json);
                return summoner;
            }
        }
        catch(Exception e)
        {
            Console.WriteLine(e.ToString());
        }
        return null;
    }

The properties are never assigned values, is it something to do with their being an outer object in the JSON when the values I need are inside the inner objects?

I'm a new programmer and so sorry if this is a silly mistake, thanks.

1 Answer 1

3

You need a wrapper for the SummonerName property that your JSON contains:

public class Wrapper
{
    public Summoner SummonerName { get; set; }
}

that you're gonna deserialize the JSON to:

public static Summoner getRecentGames(string summonerId)
{
    try
    {
        using (var webClient = new System.Net.WebClient())
        {
            var json = webClient.DownloadString("https://eu.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/"+summonerId+"?api_key="+api_key);
            var wrapper = JsonConvert.DeserializeObject<Wrapper>(json);
            return wrapper.SummonerName;
        }
    }
    catch(Exception e)
    {
        Console.WriteLine(e.ToString());
    }
    return null;
}

Also notice that your webClient instance is wrapped in a using directive - it's completely meaningless to manually call the .Dispose() method on it - that's the whole purpose of the using statement.


UPDATE:

It appears that the SummonerName property is dynamic in your JSON (which is pretty bad design of an API but anyway) and meaning that you cannot use a strongly typed wrapper.

Here's how you could handle this:

using (var webClient = new System.Net.WebClient())
{
    var json = webClient.DownloadString("https://eu.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/"+summonerId+"?api_key="+api_key);
    var summoner = JObject.Parse(json).Values().First().ToObject<Summoner>();
    return summoner;
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Darin, I have updated it accordingly. The problem I have now is that it is throwing a NullReferenceException whenever I try to output the name property of the returned summoner. I'm not sure as to why this is happening, the method isn't returning Null.
If the getRecentGames method returns a non-null Summoner instance, then calling .name on this instance won't produce a NullReferenceException. Which property is null? Are you sure that your getRecentGames method doesn't throw an exception and return null? Try debugging your code and inspect the value of the json string variable you have read from the remote endpoint as well as the properties of the deserialized Wrapper instance.
It looks like the Summoner instance that is being returned is null, is this happening because the name of the wrapper object in the JSON changes depending on the summoner id which is being searched? I have put an example here for you: gist.github.com/Coppercrawler/d0e86ff8ce2244a3fc17 - thanks again for all this help I really appreciate it.
So you are saying that the SummonerName property will be different every time so you can have {"SummonerName1":{...}} or {"SummonerName2":{...}}?
I think it is ye as the outer object in the JSON returned contains the name of the summoner which is being looked up, so when "Win" is searched the JSON is {"win":{"id":39556917,"name":"Win",.... etc
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.