我正在尝试解析JSON,该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"
}]}
然而,由于JSON的"44542152":根根据我请求的用户而变化,我发现很难将其解析为对象。
这就是我目前所拥有的:
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; }
}
}
我对此很陌生,如果问题很明显,我很抱歉,但我的目的是能够访问一些存储的属性并将它们呈现给用户。
提前谢谢。
你非常接近。
那应该是你的演员阵容中的RootObject[]。再次查看您的JSON。。。如果你适当地缩进它会更有帮助。它将一个字符串映射到一个对象数组,而不是一个对象,这些对象是你的RootObject,而不是RankInfo。
您可能需要更改嵌套类,如下所示。请告诉我这是否是故意的。
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; }
}
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[]>();
这就是我所做的:
var data = JsonConvert.DeserializeObject<Dictionary<string,IList<RankInfo.RootObject>>>( json );
var rankinfo = data["44542152"].First();