2

I'm trying to parse JSON which is returned to me in the following structure, but I just can't get the structure of the class right in order for it to parse correctly:

{"44542152": [{
   "queue": "RANKED_SOLO_5x5",
   "name": "Elise's Elite",
   "entries": [{
      "leaguePoints": 0,
      "isFreshBlood": false,
      "isHotStreak": false,
      "division": "IV",
      "isInactive": false,
      "isVeteran": false,
      "playerOrTeamName": "Autdsm",
      "playerOrTeamId": "44543152",
      "wins": 11
   }],
   "tier": "SILVER"
}]}

However because of the "44542152": root of the JSON which changes depending on the user I request, I'm finding it hard to parse this into an object.

This is what I have so far:

public static AreTheyChallenger.Core.RankInfo getLeague(string region, Summoner summoner)
    {
        try
        {
            using (var webClient = new WebClient())
            {
                var json = webClient.DownloadString("https://" + region + ".api.pvp.net/api/lol/" + region + "/v2.5/league/by-summoner/" + summoner.id + "/entry" + "?api_key=" + Keys.api_key);
                var summonerRankInfo = JObject.Parse(json).Values().First().ToObject<RankInfo>();
                return summonerRankInfo;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        return null;
    }

And this is the class I'm trying to parse it into:

public class RankInfo
{

    public class Entry
    {
        public int leaguePoints { get; set; }
        public bool isFreshBlood { get; set; }
        public bool isHotStreak { get; set; }
        public string division { get; set; }
        public bool isInactive { get; set; }
        public bool isVeteran { get; set; }
        public string playerOrTeamName { get; set; }
        public string playerOrTeamId { get; set; }
        public int wins { get; set; }
    }

    public class RootObject
    {
        public string queue { get; set; }
        public string name { get; set; }
        public List<Entry> entries { get; set; }
        public string tier { get; set; }
    }

}

I'm quite new to this and so sorry if the problem is obvious, but my intention is to be able to access some of the stored properties and present them to the user.

Thanks in advance.

4
  • 1
    Try using this to generate your classes: jsonclassgenerator.codeplex.com very handy IMO :) Commented Sep 26, 2014 at 19:11
  • The problem is obvious, but the solution certainly isn't. Good question :) Commented Sep 26, 2014 at 19:16
  • You can also use json2csharp if you don't want to download anything. json2csharp.com Commented Sep 26, 2014 at 19:17
  • Thanks for your comments :) I've been using tools like these but it turns out it didn't take the array into account, neither did I and that was the prob. Commented Sep 26, 2014 at 19:41

4 Answers 4

1

You are very close.

That should be a RootObject[] in your cast. Look at your JSON again... it's more helpful if you indent it properly. It's mapping a string to an array of objects, not a single one, and those objects are your RootObject, not the RankInfo.

Sign up to request clarification or add additional context in comments.

Comments

1

You may want to change your nested class as below. Please let me know if it was the intention.

void Main()
{
    string jsonString = "{'44542152': [{ 'queue': 'RANKED_SOLO_5x5', 'name': 'Elises Elite', 'entries': [{ 'leaguePoints': 0, 'isFreshBlood': false, 'isHotStreak': false, 'division': 'IV', 'isInactive': false, 'isVeteran': false, 'playerOrTeamName': 'Autism', 'playerOrTeamId': '44543152', 'wins': 11 }], 'tier': 'SILVER' }]}".Replace('\'','"');

    JObject jsonObject = JObject.Parse(jsonString);
    JArray jsonArray = jsonObject.Values().First() as JArray;
    var summonerRankInfo = jsonArray.First().ToObject<RootObject>();
}

public class Entry
{
    public int leaguePoints { get; set; }
    public bool isFreshBlood { get; set; }
    public bool isHotStreak { get; set; }
    public string division { get; set; }
    public bool isInactive { get; set; }
    public bool isVeteran { get; set; }
    public string playerOrTeamName { get; set; }
    public string playerOrTeamId { get; set; }
    public int wins { get; set; }
}

public class RootObject
{
    public string queue { get; set; }
    public string name { get; set; }
    public List<Entry> entries { get; set; }
    public string tier { get; set; }
}

3 Comments

This was one of the problems, the other was that I needed to take the array in the json into account.
Please review the jsonArray variable. it holds the each record of the account.
Oh sorry I overlooked that, great reply I've improved my code with this - thanks.
0

Your RankInfo class has no members, only class definitions. Perhaps you meant to have it hold an instance of a RootObject?

You can get an instance of a RootObject by slightly modifying your JSON deserialization line to the following:

var summonerRankInfo = JObject.Parse(json).Values().First().First.ToObject<RankInfo.RootObject>();

You can also do the following (if you are going to receive multiple RootObjects:

var summonerRankInfo = JObject.Parse(json).Values().First().ToObject<RankInfo.RootObject[]>();

1 Comment

Thanks so much, this in combination with the answer above solved my issue.
0

This is what i did:

var data = JsonConvert.DeserializeObject<Dictionary<string,IList<RankInfo.RootObject>>>( json );
var rankinfo = data["44542152"].First();

Comments

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.