Reputation: 47
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.
Upvotes: 2
Views: 423
Reputation: 8706
This is what i did:
var data = JsonConvert.DeserializeObject<Dictionary<string,IList<RankInfo.RootObject>>>( json );
var rankinfo = data["44542152"].First();
Upvotes: 0
Reputation: 9420
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; }
}
Upvotes: 1
Reputation: 1332
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.
Upvotes: 1
Reputation: 1319
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 RootObject
s:
var summonerRankInfo = JObject.Parse(json).Values().First().ToObject<RankInfo.RootObject[]>();
Upvotes: 0