我试图解析在以下结构中返回给我的JSON,但是我无法正确获取类的结构以便正确解析:
{"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"
}]}
但是由于" 44542152":JSON的根据我请求的用户而变化,我发现很难将其解析为对象。
这是我到目前为止所做的:
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;
}
这就是我试图解析它的课程:
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; }
}
}
我对此很新,很抱歉,如果问题很明显,但我的目的是能够访问一些存储的属性并将其呈现给用户。
提前致谢。
答案 0 :(得分:1)
你非常接近。
那应该是你演员阵容中的RootObject []。再看看你的JSON ......如果你正确缩进它会更有帮助。它将一个字符串映射到一个对象数组,而不是一个对象,这些对象是你的RootObject,而不是RankInfo。
答案 1 :(得分:1)
您可能想要更改嵌套类,如下所示。如果是意图,请告诉我。
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; }
}
答案 2 :(得分:0)
您的RankInfo
类没有成员,只有类定义。也许你打算让它拥有一个RootObject
的实例?
您可以通过稍微修改JSON反序列化行来获取RootObject
的实例:
var summonerRankInfo = JObject.Parse(json).Values().First().First.ToObject<RankInfo.RootObject>();
您还可以执行以下操作(如果您要接收多个RootObject
:
var summonerRankInfo = JObject.Parse(json).Values().First().ToObject<RankInfo.RootObject[]>();
答案 3 :(得分:0)
这就是我所做的:
var data = JsonConvert.DeserializeObject<Dictionary<string,IList<RankInfo.RootObject>>>( json );
var rankinfo = data["44542152"].First();